java_dev
java_dev

Reputation: 125

How can get CheckBox Array length in java script

in my jsp form i have one checkbox Array in my form(jsp and struts).i want to know the length of checkbox Array in java script. ex: <input:checkbox property="chkbox[]"/> . in javascript i am calling document.form[0].chkbox[].value.length but it showing javaScript error null or undefined .

Upvotes: 0

Views: 12154

Answers (2)

Zain Khan
Zain Khan

Reputation: 3793

    var checkbox = document.getElementsByName('a_checkbox')
    var ln = checkbox.length

line 1 get all the active checked boxes line 2 in the length of that array

Upvotes: 3

sushil bharwani
sushil bharwani

Reputation: 30187

if this the html

<form name='form1' >
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
<input type=checkbox name='cbox' />
</form>

javascript for that length would be

alert(document.form1.cbox.length);

or you can also use.

document.getElementsByName('cbox').length

Upvotes: 1

Related Questions