javascript events - jQuery click action does not trigger onclick attribute -
javascript events - jQuery click action does not trigger onclick attribute -
i having problems getting function clicks search matched checkboxes. click event seems fire, no checkbox gets checked nor myfunc() called.
it works when clicking each checkbox manually, wants 40+ checkboxes - not users. may wonder why utilize div-onclick instead of straight on input checkbox. because want give users larger area click on little checkbox.
i want provide users single hyperlink select multiple checkboxes match attribute value(s).
i need fast solution out of time.
html
<div id="container"> <a href="#" onclick="clickandcheckallmatch('search 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) { // stuff } function clickandcheckallmatch(value) { $('div[onclick*='+value+']').each(function(idx,e){ $('book-'+this.id).click(); }
edit when changed div label didnt have right checkbox state. div or block-element must have issue there. removed onclick *div*s, meant able target checkboxes straight , alter id values *actual_id*.
function clickandcheckallmatch(value) { $('input[onclick*='+value+']').each(function(idx,e){ // e == $(this)[0].click(); if ($(this).attr('disabled')) { //do nil } else { if ($(this).attr('checked')) { //do nil } else { $(this)[0].click(); // 1 checkbox per id } } }); } function checkall() { $('#container input[type=checkbox]:not(:checked)').each(function(idx,e){ $(this).click(); // notice difference ? }); }
rather trying utilize event alter value of checkbox, 2 things:
to give users big area click, utilize label
element, not div
. label
elements straight supported browsers, don't have create them work. there 2 ways utilize them:
<label><input type='checkbox'>foo</label>
or
<input type='checkbox' id='chk1'><label for='chk1'>foo</label>
the former nice because don't have utilize ids (which have totally unique on page, starts getting pain); latter nice markup situations can't have input
kid of label
whatever reason.
if need alter state in code, alter state straight rather trying trigger event. instance, if want alter state of checkbox chk1
, this:
$('#chk1').attr("checked", true); // or false, of course of study
...or clear of checkboxes in container (useful "all" or "none" links):
$('#container input[type=checkbox]').attr("checked", false); // or true, of course of study
jquery javascript-events event-handling jquery-click-event
Comments
Post a Comment