Determining if a field exists in the form

I have a form field (a series of flags) that is created dynamically from the database, so it is possible that this field will not exist on the form (if the database does not have the corresponding values). I have code that needs to be executed based on whether a field exists, and pull out the values ​​that are selected if they exist. I cannot get javascript to recognize that this field exists. Here is what I tried:

function displayAction(){ var f = document.adminForm; var a = f.action; if(f.prefix.value!="-") { a = a + '&task=callExclusionDisplay&prefix=' + f.prefix.value; } else { var exclusions = document.getElementById("exclusions"); if (exclusions != null){ alert("exclusions set"); a = a + '&task=callExclusionCreate&prefix=' + f.prefix.value + '&exclusions=' + exclusions.join(); } } alert('after if, action is ' + a); } 

The code never passes an if statement that checks if an exception is an exception, although when viewing a page there are several flags with exception names (with an identifier also set for exceptions). Is there a problem with! = Null because it is a group of checkboxes and not a single form element? How can I make this work? If I skip the null test, the code throws exception errors that are not detected if the database does not return any corresponding values.

+4
source share
5 answers

You are using document.getElementById , but form elements have a name. Try f.elements.namedItem("exclusions") instead of exclusions != null

+2
source

Multiple elements on the same page cannot share the id attribute (i.e. id must be unique or unset). In addition, although some (older) browsers mistakenly collect elements whose name matches the identifier viewed with getElementById , this is not valid and will not work with a cross browser.

If you want to get a group of elements, you can give them all the same name attributes and use document.getElementsByName to get the group. Note that the result will be a NodeList , which looks like an array in which it can be repeated.

+1
source

Do all flags have the same id == exceptions? If so, you must first fix it.

Before you do this, have you tried checking the first checkbox and see if the if condition passes?

0
source

If you have multiple elements with an "exception" id, this will ruin the functionality of getElementById. I would remove duplicate "exceptions" from all of your items and use getElementByName () instead and provide name = group of exclusion flags.

Edit: But it is much easier to use jQuery , and this gives you a guarantee of compatibility with multiple browsers. To do the same with jQuery , do the following:

 var checkBoxesExist = $('[name=exclusions]').count() > 0; 

Or, if you provided your unique item identifiers, you can do this:

 var checkbox1exists = $('#checkBox1').count() > 0; 
0
source

Each item must have a unique identifier.

Then you can check like this:

 if (document.getElementById('exclusions1')) { //field exists } 

Or, if you need to go through their group:

 for (x=0; x<10; x++) { if (document.getElementById('exclusions' + x)) { //field X exists } } 
0
source

All Articles