bobetko
bobetko

Reputation: 5167

Unchecking of all checkboxes works only once using jquery

I have really interesting problem. I am trying to uncheck all checkboxes on my page. All checkboxes have IDs starting with "chkbx_".

I tried doing this:

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

and this:

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

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

Basically, I am trying to make checkboxes to behave like radio buttons. When I check one I run function that receive this (ref.) as parameter, and I first uncheck all checkboxes, and then I check one that is clicked.

I was also trying to use this to go through all checkboxes to see if they are checked:

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

Even though there are 4 checkboxes, above loop executes only once, for first checkbox on the page.

What is wrong? Thanks

Upvotes: 4

Views: 1790

Answers (2)

Jyoti Yadav
Jyoti Yadav

Reputation: 106

use prop instead of attr

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

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

Upvotes: 0

Marco Johannesen
Marco Johannesen

Reputation: 13134

Something else must be wrong.

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

Works fine? :)

Upvotes: 1

Related Questions