How to pass data from client script to html in UI page

Shivam32
Tera Contributor

Hi,

 

I have a UI Page where I need some advice/help on how can we display the answer retrieved in Client script and need to be passed to HTML as soon as a the page loads?

Right now, I have the info in my client script which is coming from Script include but unable to display it in UI PAGE as not sure on how can we pass the value to HTML.

 

My code below:

 

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">
<input type="hidden" name="userId" id="userId" value="${sysparm_user_id}" />
<div>
<p id="answer"> User Status</p>
</div>
 
</j:jelly>
 
Client Script:
function checkTest) {
if(!gel('userId').value) {
alert('userId not defined');
}
var userId = gel('userId').value;
var ga = new GlideAjax("sn_hr_core.test");
ga.addParam('sysparm_name', 'getSubjectPerson');
ga.addParam('sysparm_user_id', userId);
ga.getXMLAnswer(function(answer) {
gel('answer').value = answer;
console.log('answer' +answer);
});
}
checkTest(); // run on Load
4 REPLIES 4

SN_Learn
Kilo Patron
Kilo Patron

Hi @Shivam32 ,

 

Please try the below suggested ways:

UI Page - How to pass value from Client Script to HTML 

Pass value from Client Script to UI Page 

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Soni Tushar
Tera Guru

Hello @Shivam32 ,

 

Please refer below link.

https://www.servicenow.com/community/developer-forum/ui-page-how-to-pass-value-from-client-script-to....

 

If you found my response helpful, please consider marking it as "Helpful" or "Accept Solution." Thank you! 

 

Shivam32
Tera Contributor

@Ankur Bawiskar Could you please suggest. How can we fix this?

Ankur Bawiskar
Tera Patron
Tera Patron

@Shivam32 

try this -> don't set mention any text within paragraph tag; set it via script from client script

HTML: Update as this

<p id="answer"></p>

this will work in client script

document.getElementById('answer').innerHTML= 'User Status ' + answer;

OR

document.getElementById('answer').innerText= 'User Status ' + answer;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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