Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Pass data from HTML Javascript to Server Script in portal widget

SD35
Tera Expert

I have a HTML like this: 

<input class="chkbx" id="dt_1" ng-model="c.dt_1" type="checkbox" name="dt_1" onchange="chkVal()"/>

<script>

function chkVal(){
var checkBox1 = document.getElementById('dt_1');
if (checkBox1.checked == true){

//pass a value from this javascript to server script

}
else{

//pass a value from this javascript to server script

   }
}

</script>

1 ACCEPTED SOLUTION

Shishir
Mega Guru

Hi,

Instead of DOM manipulation use the ng-model variable and use the same in client controller and pass on the values to the server script.

HTML:

<input class="chkbx" id="dt_1" ng-model="c.data.dt_1" type="checkbox" name="dt_1" onchange="chkVal()"/>

Client Controller:

function chkVal(){
if (c.data.dt_1 == true){

c.data.checkbox =true;

}
else{

c.data.checkbox =false;

}

c.server.update();
}

Server Side:

if(input&&input.checkbox ){

gs.addinfomessge(input.checkbox);

}

 

 

View solution in original post

2 REPLIES 2

AnirudhKumar
Mega Sage

Change your code to:

 

HTML:

<input class="chkbx" id="dt_1" ng-model="c.dt_1" type="checkbox" name="dt_1" onchange="c.chkVal()"/>

 

Client Controller:

c.chkVal = function(){
var checkBox1 = document.getElementById('dt_1');
if (checkBox1.checked == true){

c.data.yourVariableToPass = 'ServiceNow rockz';
c.server.update();

}
else{

c.data.yourVariableToPass = 'ServiceNow rockz';
c.server.update();

   }
}

 

Server Script:

if(input)
{
  if(input.yourVariableToPass)
    {
      gs.addInfoMessage(input.yourVariableToPass);
    }
}

Shishir
Mega Guru

Hi,

Instead of DOM manipulation use the ng-model variable and use the same in client controller and pass on the values to the server script.

HTML:

<input class="chkbx" id="dt_1" ng-model="c.data.dt_1" type="checkbox" name="dt_1" onchange="chkVal()"/>

Client Controller:

function chkVal(){
if (c.data.dt_1 == true){

c.data.checkbox =true;

}
else{

c.data.checkbox =false;

}

c.server.update();
}

Server Side:

if(input&&input.checkbox ){

gs.addinfomessge(input.checkbox);

}