Determining If A Field Exists On A Form
Solution 1:
You're using document.getElementById
, but form elements have a name.
Try f.elements.namedItem("exclusions")
instead of exclusions != null
Solution 2:
Multiple elements in the same page cannot share an id
attribute (ie. id
must be unique or unset). As well, though some (older) browsers erroneously collect elements whose name matches the ID being looked for with getElementById
, this is invalid and will not work cross-browser.
If you want to get a group of elements, you can give them all the same name
attribute, and use document.getElementsByName
to get the group. Note that the result of that will be a NodeList
which is kind of like an array in that it can be iterated over.
Solution 3:
Do all the checkboxes have the same id == exclusions? If yes, then you must first correct that.
Before you do so, did you try checking the first checkbox and see if the if condition goes through?
Solution 4:
if you have more than one element with the id "exclusions" it will screw up the functionality of getElementById. I would remove the duplicate "exclusions" ids from all of your elements and use getElementByName() instead, and give your group of checkboxes the name="exclusions" instead.
Edit: But there is a much simpler way using jQuery, and it gives you some cross browser compability guarrantee. To do the same thing with jQuery do this:
var checkBoxesExist = $('[name=exclusions]').count() > 0;
Or if you have given your elements unique ID's then you can do this:
var checkbox1exists = $('#checkBox1').count() > 0;
Solution 5:
Each element must have a unique ID.
Then, you can check just like this:
if (document.getElementById('exclusions1')) {
//field exists
}
Or if you need to loop through a bunch of them:
for (x=0; x<10; x++) {
if (document.getElementById('exclusions' + x)) {
//field X exists
}
}
Post a Comment for "Determining If A Field Exists On A Form"