Mostafa Elkady
Mostafa Elkady

Reputation: 5801

Problem with testing for checkbox being marked

I have an input array like

<input id='delete[18]' name="delete[18]" value="1" type="checkbox">
<input id='delete[19]' name="delete[19]" value="1" type="checkbox">

This Javascript code for check does not work. Why?

if (delete_question.19.checked == 1) {
    if (confirm('msg')) {
        return true;
    } else {
        return false;
    }
}

Upvotes: 2

Views: 67

Answers (2)

Quentin
Quentin

Reputation: 944014

Because the DOM for HTML forms doesn't work anything like PHP's form data parsing engine.

You want something more like:

document.forms.id_of_form.elements['delete[19]']

Note: Use of square bracket notation because dot notation can't access properties that include [ or ] characters.

BTW, an HTML id cannot include the characters [ or ] (although names can) so the HTML is invalid.

Upvotes: 4

Jacob Relkin
Jacob Relkin

Reputation: 163288

Try this:

if (document.getElementById('myForm')['delete[19]'].checked) {
   return confirm('msg');
}

Upvotes: 4

Related Questions