How to use DOM or Jquery in client script

Ben Wang1
Kilo Contributor

This is Catalog Client script screenshot.  i want to use JQuery in this client  script. I add the 'Isolate script' field, but it still doesn't work

find_real_file.png

4 REPLIES 4

Azim Kazi
Giga Guru

Both prototype and jquery can't handle id selectors that have periods (.) in them.

 

 

 

To get the selectors to work you need to escape (use \) the special characters.   This also applies to id's that have # and : in them.

 

The correct   prototype selector statement is :

 

 

 

$$('#element\.incident\.comments div').each( ....

 

Azim Kazi
Giga Guru

Hello Ben,

This is working for me:

 

 

 

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!

 

 

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);


}


});


}

if my response helps you then kindly mark my answer helpful and correct otherwise if any query feels free to ask further.

 

Regards,

Ajim.

 

Hi Ajim,

i try to use your script with my client script. but some mistakes see below pic.this is my portal page error.

find_real_file.png

Swapnil Soni1
Giga Guru

Hi,

You can try this

Sample function:

var gURL = new GlideURL();

 

gURL.setFromCurrent();

 

alert(gURL.getParam("sysparm_id"));

 

where sysparm_id is the parameter that needs to be read from the URL.

 

Or you can go through the link-

https://community.servicenow.com/community?id=community_question&sys_id=3c5a8ba9db5cdbc01dcaf3231f96...

Please mark correct or helpful.

Thanks

Swapnil