I have a simple HTML files with book seats field as array , the code below
<input type="checkbox" name="book[]" value="<?php echo $i; ?> /><?php echo $i; ?>
<input type="submit" value="BOOK" onclick = "checkIfSeatEmpty()" />
The javascript I used to validate if the field book
is empty is:
var x=document.forms["bkfrm"].getElementById("chkbx").value;
//alert(x); alerts 'undefined' !
// var x=document.forms["bkfrm"].getElementById("book").length;
alert(x);
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
return TRUE;
But the above code alerts as undefined
! How can I check if the field is empty?
document.forms["bkfrm"]
before callinggetElementById
. Element ids should be unique thusdocument.getElementById('chkbx')
should be sufficient. If you have multiple elements with the same id in a single page you are generating invalid code. Also, usingdocument.getElementById
instead ofdocument.forms["bkfrm"].getElementById
will be faster too.