Good day.

Inside the div there are several label with checkbox and span . In the original checkboxes themselves are hidden - there is only a span by which you can click and influence through it the checkbox.

With jquery, I'm trying to find all checked checkboxes. And if at least one is found, I bring its class to the array.

The problem - everything works with a delay of one click. If you click on the checkbox, the jquery works faster than the checkbox is checked. How to solve it? I understand that you can: when you click on the span to see what state is associated with it sheckbox. If it is not pressed, then in the script we will assume that it is pressed, etc. But these are some kind of twists ... Is it possible to do this somehow usually?

 jQuery("span").click(function(event) { my_arr = new Array(); jQuery('#all input:checkbox:checked').each(function() { my_arr.push($(this).attr('class')); return false; }); console.log(my_arr); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='all'> <label> <input type="checkbox" name="filter" class="hull" value="to92"> <span class="hull">to92</span> </label> <label> <input type="checkbox" name="filter" class="hull" value="to91"> <span class="hull">to91</span> </label> </div> 

    1 answer 1

     jQuery("input:checkbox").change(function(event) { my_arr = []; jQuery('#all input:checkbox:checked').each(function() { my_arr.push($(this).attr('class')); return false; }); console.log(my_arr); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='all'> <label> <input type="checkbox" name="filter" class="hull" value="to92"> <span class="hull">to92</span> </label> <label> <input type="checkbox" name="filter" class="hull" value="to91"> <span class="hull">to91</span> </label> </div> 

    • thank! dupe I) After a couple of minutes, I will mark the issue as resolved. - n.osennij
    • @ n.osennij Not at all. But the question is well asked. Good luck. - Igor