Unchecking all checkboxes works only once with jquery

I have a really interesting problem. I am trying to uncheck all the checkboxes on my page. All flags have identifiers starting with "chkbx _".

I tried to do this:

$('input[id^="chkbx_"]').removeAttr("checked"); 

and this:

 $('input[id^="chkbx_"]').attr("checked", false); 

It works only once! The second time, it seems, it is ignored.

Basically, I'm trying to make checkboxes to behave like switches. When I check one, I run a function that receives this parameter (ref.) As a parameter, and first uncheck all the checkboxes, and then check the one that clicked.

I also tried using this to go through all the checkboxes to check if they are checked:

 $('input[id^="chkbx_"]').each(function () { ... }); 

Despite the presence of 4 flags, the above loop runs only once, for the first flag on the page.

What's wrong? Thanks

+4
source share
2 answers

Something else must be wrong.

Sketched this example: http://jsfiddle.net/nzmv8/

Works great? :)

+1
source

use prop instead of attr

  • $ ('input [id ^ = "chkbx _"]'). prop ("checked", false);

  • $ ('input [id ^ = "chkbx _"]'). prop ("checked", true);

0
source

Source: https://habr.com/ru/post/1379718/


All Articles