You are viewing limited content. For full access, please sign in.

Question

Question

document.getElementByID value undefined

asked on September 6, 2018

Hello,

I have a lookup-populated integer that I want to reference as a variable. However, the following returns as undefined. It must be a syntax issue, but I cannot identify it. Any help is greatly appreciated.

 

$(document).ready(function() {
    $("#Field43").on('change', evaluate);
    var sna = document.getElementByID('Field47').value;
    var decS = 6 - sna;
    
    
    function evaluate(){
    
      alert(sna);  

...
      }

 

0 0

Answer

SELECTED ANSWER
replied on September 6, 2018

Because JQuery is loaded in Forms you could do the following instead

var sna = $("#Field47").val();

Just make sure that's the correct field identifier.

However, a problem with the evaluate function may be that the sna variable doesn't exist within the scope of that function.

What exactly is returned as undefined? That will help determine the actual problem.

 

Another option which I typically favor is to add custom classes to my fields. As an example, I would add something like "snaField" to the CSS class attribute in the field configuration, then reference it with the following JQuery.

var sna = $('.snaField input').val();

What I like about this approach is that you can make the classes descriptive so it is easier to understand the code, and it removes the need to track down the Field# ids for all of your form elements.

The only change you need to make is to replace "input" with the appropriate element type if it is a different field type (select for dropdowns, textarea for multi-line, etc.).

0 0

Replies

replied on September 6, 2018

Assigning a CSS class worked perfectly, thank you! I also moved to global variables to the function, so I could call them locally.

 

Thanks for your help!

$(document).ready(function() {
    $("#Field43").on('change', evaluate);
       
    function evaluate(){
    var sna = $('.snaField input').val();
    var decS = 6 - sna;

...

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.