Forum Moderators: open

Message Too Old, No Replies

how do i get this to work with javascript: name="xyz[]"

         

thejwb1

12:03 am on Sep 1, 2009 (gmt 0)

10+ Year Member



I am trying to get my check all function to work but it is not working when the [] is in the code: name="state[]".

The [] is required for the php form.

Thanks.

daveVk

5:29 am on Sep 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are there other places in your Javascript where state is refered to ?

By itself the name attribute you have should be Ok on a input element, but care is needed in referencing it.

rocknbil

5:30 pm on Sep 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



trying to get my check all function to work

You can/should assign ID's to these elements

<input type="checkbox" name="[]" id="some-unique-id" value="some-value">

Then reference them by document.getElementById('some-unique-id').

OR you can iterate through the form, and get them by form element type.

<input type="checkbox" name="chkson" id="chkson" value="1" onClick="checkBoxes(this.form,1);"> All on
<input type="checkbox" name="chksoff" id="chksoff" value="1" onClick="checkBoxes(this.form,0);"> All off


function checkBoxes(form,onoff) {
var sw = (onoff==1)?true:false;
for (i=0;i<form.elements.length;i++) {
var obj = form.elements[i];
if ((obj.type=='checkbox') && (obj.name != 'chkson') && (obj.name != 'chksoff')) {
obj.checked = sw;
}
}
if (onoff == 1) { form.chksoff.checked = false; }
else { form.chkson.checked = false; }
}

Note that this still references "name" but only if the names are the on/off checks. Also note that any other checkboxes in the form will be affected, so you may want to add ones you don't want affected by this to the "if" conditional that skips the on/off checks.