How to use jQuery on client script section of UI page

yogi4
Kilo Explorer

I have created a html button on html section of UI page and wanted to call script include on click of this button. I am using jQuery to call script include on click on this button.

*****html section of UI page

<button id="buttonID" >Add Record</button>

****client Script section of UI page*******

jQuery('#buttonID').click(function(){
var ga = new GlideAjax('NameOfScriptInclude');
ga.addParam('sysparm_name','functionName');
ga.getXML(funcCallback);
});

function funcCallback(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
}

Somehow the nothing is happening on button click. Do I have to include some jQuery reference anywhere to make it work

2 REPLIES 2

ChrisBurks
Mega Sage

That should work. Have you checked for any errors by doing an inspect?

If there are no errors then it may be the Script Include that has errors such as the "Client callable" box not being checked in the Script Include form.

Or if on the UI page the "Direct" box is checked then, yes you would have to bring in the jQuery library.

 

Rick Marsha
Giga Expert

Hi yogi,

Your code snippet worked for me using a simple script include, so not sure why we have different results.

However, here is the sample code that works form me, along with a second button that uses a different approach just in case you're unable to resolve the issue.

 

******** HTML
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<button id="buttonID1" >Button 1</button>
<button id="buttonID2" onClick="btn2_click();">Button 2</button>
<input id="btnResponse"/>
</j:jelly>

 

******** Client script
// BUTTON 1
jQuery('#buttonID1').click(function() {
var ga = new GlideAjax('rmTestLogger');
ga.addParam('sysparm_name','logItNow');
ga.addParam('sysparm_theparm', 'button1');
ga.getXML(funcCallback1);

});

function funcCallback1(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
jQuery('#btnResponse').val('called_back_1');

}

// BUTTON 2
function btn2_click() {
var ga = new GlideAjax('rmTestLogger');
ga.addParam('sysparm_name','logItNow');
ga.addParam('sysparm_theparm', 'button2');
ga.getXML(funcCallback2);

}

function funcCallback2(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
jQuery('#btnResponse').val('called_back_2');

}

 

********* Script include

Client callable = true

var rmTestLogger = Class.create();
rmTestLogger.prototype = Object.extendsObject(AbstractAjaxProcessor, {
logItNow: function (parm) {
if (!parm) parm = this.getParameter('sysparm_theparm');
gs.info('rmTestLogger: #' + parm + ' triggered');
return true;

},

});

 

Thanks,

-Rick