0

I have several groups of checkboxes, each group has unique class name, I would like to select all function for each group. I have a JavaScript function like this:

function toggle(source) {
    var checkboxes = document.getElementsByClassName(classname);
    for(var i=0, n=checkboxes.length;i<n;i++) {
        checkboxes[i].checked = source.checked;
    }
}

It works when <INPUT type="checkbox" onClick="toggle(this)" /> and I hard coded class name in javascript function.

I tried:

<INPUT type="checkbox" onClick="toggle(this,<?php echo $key ?>)" />

And change javascript function to function toggle(source, classname){...} and toggle() then use arguments[0] for source, arguments[1] for classname in original function. Neither of them worked.

What is the right way to add parameter to this function?

1
  • Why are you writing the element with all caps? Commented Jan 31, 2016 at 3:06

1 Answer 1

1

I think you missed some quotes:

<INPUT type="checkbox" onClick="toggle(this,'<?php echo $key ?>')" />
Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help :) Please consider accepting this answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.