I am having problems getting a function that clicks all search matched checkboxes. The click event seems to fire, but no checkbox gets checked nor is myFunc() called.
It works perfectly when clicking each checkbox manually, but who wants to do that with 40+ checkboxes - not my users.
You may wonder why I use a div-onclick instead of directly on the input checkbox. That is because I want give my users a larger area to click on than a small checkbox.
I want to provide my users with a single hyperlink to select multiple checkboxes that match certain attribute value(s).
I need fast solution as I am out of time.
HTML
<div id="container">
<a href="#" onclick="clickAndCheckAllMatch('search for this text');return false;">some text</a>
<div id="*actual_ID*" onclick="myFunc(a, b, c, d);">
<input type="checkbox" id="book-*actual_ID*" onclick="this.parentNode.click;return false;">
</div>
</div>
JS
function myFunc(a, b, c, d) {
// does stuff
}
function clickAndCheckAllMatch(value) {
$('div[onclick*='+value+']').each(function(idx,e){
$('book-'+this.id).click();
}
EDIT
When I changed from div to label then I didnt have to correct the checkbox state. A div or block-element must have some issue there.
I also removed the onclick from the *div*s, which meant I was able to target the checkboxes directly and could change their ID values to just the *actual_ID*.
function clickAndCheckAllMatch(value) {
$('input[onclick*='+value+']').each(function(idx,e){
// e == this
$(this)[0].click();
if ($(this).attr('disabled')) {
//do nothing
} else {
if ($(this).attr('checked')) {
//do nothing
} else {
$(this)[0].click(); // only one checkbox per ID
}
}
});
}
function CheckAll() {
$('#container input[type=checkbox]:not(:checked)').each(function(idx,e){
$(this).click(); // notice the difference ?
});
}