Wednesday, 15 June 2011

javascript - jQuery: change visual output of checkbox to checked -



javascript - jQuery: change visual output of checkbox to checked -

when loop on checkboxes class .category attached , seek alter visual output of checkbox checked, nil happens. variable 'state' returns true or false , used alter element it's current visual output. @ moment nil happens , don't know why.

html:

<input type="checkbox" class="category">checky 1</span> <input type="checkbox" class="category">checky 2</span> <input type="checkbox" class="category">checky 3</span>

js:

$('body').on('click','.category',click); function click(){ $('.category').each(function(i,element){ var state = $(element).prop('checked'); $(element).prop('checked', state); }); homecoming false; }

thanks in advance.

note nil happens because you're using return false in event handler. prevent (un)checking checkbox.

if you're trying implement select all option, do:

class="snippet-code-js lang-js prettyprint-override">$(".category:first").on("click", function() { $(".category").prop("checked", this.checked); }); class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label><input type="checkbox" class="category"/> select all</label><br> <label><input type="checkbox" class="category"/></label><br> <label><input type="checkbox" class="category"/></label><br> <label><input type="checkbox" class="category"/></label><br> <label><input type="checkbox" class="category"/></label><br> <label><input type="checkbox" class="category"/></label><br> <label><input type="checkbox" class="category"/></label><br>

javascript jquery checkbox

No comments:

Post a Comment