On Load client script with jQuery problem setting value

diogo_ramos
Mega Expert

Hi guys I need to create the following behavior,

When a user clicks on the comments field (if they are empty) a signature must be added to the field (this signature is a string in the sys_user table that I get using ajax)

I currently have it working with jQuery as per follows:

If you look at line 25 and 26, it's where I set the value, what happens is that if I use g_form to set the value it only works the first time (when the user goes to the field and clicks it), if for some reason he deletes everything and goes out of focus of that field and then he clicks it again it won't work the second time, that's why I have the line 26 which works fine.

Is there a better way to achieve this or should I just use the second case?

Thanks

Diogo

function onLoad() {

// Store the variable on load

var signatureValue ="";

jslog("in the script ");

var j_id = $j('#activity-stream-comments-textarea'); //comments ID

// get value from the server

var ga = new GlideAjax('getMySignature');

ga.addParam('sysparm_name','getMySignature');

ga.addParam('sysparm_user_id',g_user.userID);

ga.getXML(answerParse);

function answerParse(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");

signatureValue = answer; // variable with the value stored

}

j_id.focus(function() { // when user clicks the field

var content = j_id.val();

if (content)

jslog("content has something");

else {

jslog("content does not have anything SETTING SIGNATURE !!");

g_form.setValue("comments",signatureValue);}

//j_id.val(signatureValue);

});

}

1 ACCEPTED SOLUTION

sndangibbard
Mega Guru

Hi Diogo,



This is working for me:



function onLoad() {


// Store the variable on load


var signatureValue ="Hello!";



//**Additional comments field can be one of two elements:


//#activity-stream-comments-textarea


//#activity-stream-textarea (initial)



var el = '#activity-stream-textarea';


jQuery('body').on('focus', '#activity-stream-textarea,#activity-stream-comments-textarea', function(e){


//dynamically set current element


var el = jQuery('#activity-stream-comments-textarea').is(':visible')


?'#activity-stream-comments-textarea':'#activity-stream-textarea';



var content = jQuery(el).val();



if(!content.length){


jQuery(el).val(signatureValue);


}


});


}










The comments text-area actually changes as you interact with it so it can have two different IDs. This version of your script simply binds the focus event to both elements and checks which is the currently active element in the event handler.



Hope this helps!


View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Diogo,



Since this is onLoad script it will run only once. The point that it is working with jquery is because jquery focus on that field. Whenever focus is there on that field it will work.


So I would recommend to have the second approach and test the functionality again to verify.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

sndangibbard
Mega Guru

Hi Diogo,



This is working for me:



function onLoad() {


// Store the variable on load


var signatureValue ="Hello!";



//**Additional comments field can be one of two elements:


//#activity-stream-comments-textarea


//#activity-stream-textarea (initial)



var el = '#activity-stream-textarea';


jQuery('body').on('focus', '#activity-stream-textarea,#activity-stream-comments-textarea', function(e){


//dynamically set current element


var el = jQuery('#activity-stream-comments-textarea').is(':visible')


?'#activity-stream-comments-textarea':'#activity-stream-textarea';



var content = jQuery(el).val();



if(!content.length){


jQuery(el).val(signatureValue);


}


});


}










The comments text-area actually changes as you interact with it so it can have two different IDs. This version of your script simply binds the focus event to both elements and checks which is the currently active element in the event handler.



Hope this helps!


Hey Dan your solutions works fine, just have one question, was just testing and when I have the text in the box, If I don't do anything and click save it won't save into the ticket (this shouldn't happen as the users will always write comments) but do you know how can we go arround this ?



Thanks both for your replies.



Cheers,


Diogo




edit:



I got this working using a On change script :



function onChange(control, oldValue, newValue, isLoading, isTemplate) {




// When calling setValue from the scope isLoading the signaturevalue has a value if called outside it will be empty


function setValue(){


if (g_form.getValue("comments") == "") {


jslog("content does not have anything SETTING SIGNATURE !! content is " + g_form.getValue("comments"));


g_form.setValue("comments",   signatureValue);


} else


jslog("not empty " + g_form.getValue("comments"));


}




function getSignature() {



var ga = new GlideAjax('getMySignature');


ga.addParam('sysparm_name','getMySignature');


ga.addParam('sysparm_user_id',g_user.userID);


ga.getXML(answerParse);



function answerParse(response) {


var answer = response.responseXML.documentElement.getAttribute("answer");


signatureValue = answer;


}


}



if (isLoading)


{


var j_id = $j('#activity-stream-comments-textarea');


$j('label[for="activity-stream-comments-textarea"]').click(function(e) {


e.preventDefault(); // prevents logic to run when clicking label instead of box


});



j_id.click(function() {


// when user clicks the field


setValue();


});



getSignature();


}



var signatureValue = "";


var content = g_form.getValue("comments");




if (isTemplate){ // if a template is used also add the signature to the end of the comments (the get clientdate is used when the ajax is called so we don't have to call the script again)


var templateComment = g_form.getValue("comments");


g_form.setValue("comments", templateComment + "\n\n" + g_user.getClientData('test1'));


}



if (newValue == ""){


setValue();


}


}




















This is because setting the value of the text-area element doesn't actually change the value as far as the form is concerned. You could remedy this by adding


g_form.setValue('comments', signatureValue) at line 19