javascript - how to apply function to each checkbox on page? -


i trying apply function every checkbox on page shows/hides <div class="selectlist"> depending on if checkbox checked, function makes <div class="selectlist"> on page toggle

$("input[type=checkbox]").live('change', function() {   if ($(this).is(':checked') == false) {    $('#selectlist').hide();     } else {        $('#selectlist').show();     }  });      

i tried jquery each function doesnt seem work

    $.each($("input[type=checkbox]").live('change', function() {          if ($(this).is(':checked') == false) {              $('#selectlist').hide();         } else {              $('#selectlist').show();         }     })); 

i know possible using class instead of input[type=checkbox] want avoid doing that

how can make jquery change behavior of checkbox user clicks?

if you're trying bind event handler elements verifying input[type=checkbox],

$(document).on('change', "input[type=checkbox]", function() {     if (!this.checked) {          $('#selectlist').hide();     } else {          $('#selectlist').show();     } }); 

no need use each there : jquery functions work if jquery set contains more 1 element.

note use on there instead of live : after having been deprecated long time, live has been removed recent versions of jquery.


edit : discussion in comments below lead code :

$(document).on('change', "input[type=checkbox]", function() {     $(this).next().toggle(this.checked); }); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -