Jump to content

[SOLVED] javascript conflict with object name itmchk[]..


Dragen

Recommended Posts

Hi,

I'm not too brilliant with javascript, as I mainly codse in php.. leaving javascript alone if I can.

I've got a series of checkboxes, all named 'itmchk[]' The [] is so that my php code will read it as an array.

 

Now I'm using javascript so when I select one checkbox it checks them all.

function checkAll(checkname, exby) {
  for (i = 0; i < checkname.length; i++)
  checkname[i].checked = exby.checked? true:false
}

Then the checkbox to check the others looks like this:

<input type="checkbox" onclick="checkAll(document.editlist.itmchk[],this);" />

 

Now my problem is javascript seems to get a conflict from the [] in the check name. If I remove it from the checkboxes it will work fine, but I need it there for my php array.

Is there any way to stop javascript conflicting with []?

 

Thanks

Javascript wont know how to parse that...

 

 

Try this (assuming editlist is name of form?)

document.editlist['itmchk[]']

 

or

document.forms[0].elements['itmchk[]']

 

or

document.forms[0]['itmchk[]']

 

or

document.forms['editlist']['itmchk[]']

 

one of those WILL work.

thanks. I've actually found a way round it which seems to work:

function ckeck_uncheck_all() {
        var frm = document.editlist;
        for (var i=0; i < frm.elements.length; i++) {
                var elmnt = frm.elements[i];
                if (elmnt.type == 'checkbox') {
                        if(frm.checkall.checked == true){ elmnt.checked=false; }
            else{ elmnt.checked=true; }
                }
        }
        if(frm.checkall.checked == true){ frm.checkall.checked = false; }
    else{ frm.checkall.checked = true; }
}

 

then the checbox has this code:

<input name="checkall" type="checkbox" class="checkbox" onclick="ckeck_uncheck_all();" />

which seems to check and uncheck every checkbox in the named form.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.