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.

addEventListener not working in Service Portal

Manar Elkhalifa
Tera Contributor

 

Hi everyone,

I'm working with a catalog item and trying to perform a calculation whenever a user types into a single-line text box variable. Here's what I did:

 

var s = g_form.getControl("variable_name");
s.addEventListener('keyup', functionName);

function functionName() {
  alert('inside function');
}

This works perfectly in the Catalog Item (Try It) view.

However, when I try the same in the Service Portal, it throws an error:
TypeError: s.addEventListener is not a function.

I understand that onChange only triggers when the field loses focus or when the user clicks elsewhere, which is why I used addEventListener to detect keystrokes directly.

Does anyone know why this approach works in the regular catalog view but not in the Service Portal? Or is there a recommended way to handle keyup or live input tracking in the Service Portal?

Thanks in advance for your help!

1 REPLY 1

Teo Gamarra
Tera Expert

The issue you're having is because in catalog client script .addEventListener is not a function. Therefore, most likely in your front view console you will see a log stating:

 Events.js:100 wrong listener type: undefined

 

Tested on my end, set the script within a catalog client script. In order to get the actual DOM element, you'd need to modify your widget DOM entrance in Portal. 

 

TeoGamarra_0-1762811239410.png