0

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?

4
  • 2
    There is no such id 'chkbx' in your code. Commented Mar 7, 2012 at 12:18
  • Just add id="book". In your code there is only name property but no id property. getElementById search elements by id. Commented Mar 7, 2012 at 12:23
  • You can cehck this one Javascript: Get all elements with id id Commented Mar 7, 2012 at 12:28
  • There should be no need to navigate to document.forms["bkfrm"] before calling getElementById. Element ids should be unique thus document.getElementById('chkbx') should be sufficient. If you have multiple elements with the same id in a single page you are generating invalid code. Also, using document.getElementById instead of document.forms["bkfrm"].getElementById will be faster too. Commented Mar 7, 2012 at 12:31

2 Answers 2

0

try like following

var x=document.forms["bkfrm"].getElementById("book").checked;
0

there is no such id as chkbx or book in your html code.. use the following code

Jquery

$("input[name='book']:checked").val();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.