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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.