PDA

View Full Version : length is null or not an object error in IE8



jebaraj747
09-27-2011, 07:21 AM
Hi,
I have added an validation script for Pizza menu. Validation working fine in all browsers except IE.
When I working in IE8 it throws an error "length is null or not an object error in IE8".
Please help me. See Java script code below:
<script>
function UnCheckRadios(radioButtonGroupName)
{

var formName = "frmOrder";
var form = document.forms[formName];
alert(form);
var noOfRadioButtons=form[radioButtonGroupName].length;
for(var x=0;x<noOfRadioButtons;x++)
{
chk=form[radioButtonGroupName]
.checked=false;

}
}
</script>

PHP code:
<input type="radio" id="<?=$subitem['subgroup_id'];?>_comboleft" name="<?=$subitem['subgroup_id'];?>_comboleft" value="<?=$subitem['id'];?>:Left Side" onclick="UnCheckRadios('<?=$subitem["subgroup_id"];?>_combowhole');">

Kor
09-27-2011, 03:55 PM
In classical JavaScript reference model, a group of radio buttons has no length if there is only a single button. Beside any javascript explanation (the ambiguity between single elements and a group of elements with the same name), it looks obvious that radio buttons should be used only in a group, not isolated.

Anyway, there are several ways to solve that. The simplest, probably, is to use the DOM 1 reference document.getElementsByName(). That will return a collection in any situation


function UnCheckRadios(radioButtonGroupName){
var radios=document.getElementsByName(radioButtonGroupName), r, i=0;
while(r=radios[i++]){
r.checked=false;
}
}

And: important - your code shows that you are giving both an id and a name to your elements. But name and id are different tokens, as, unlike the name, the id must be unique on document. Remove the id.