0

EDIT This question is NOT a dupe of Setting "checked" for a checkbox with jQuery? because this question here specifically asks about the special case where: "Once a user clicks on a checkbox, that checkbox stops responding to the "checked" attribute changes.".

There's one answer (one with few upvotes compared to the other) that talks about that, but all the links in that answer are utterly broken...

I'm trying to do something "out of curiosity". There's no point in what I'm trying to do.

I successfully used JQuery to display if a checkbox just got checked or unchecked using an alert. The following works fine:

$(document).ready(function() {
  $('.cb').change(function(){
    if ($('.cb').is(':checked')) {
      alert('changed to checked');
    } else {
      alert('changed to unchecked');
    }
   });
});

However I can't figure out how to turn back the checkbox to its initial state (once again: there's no point in doing that, but I want to do it).

Is it possible to do that and will is it possible to toggle back the checkbox to its original state without triggering the change event?

I tried attr and removeAttr to no avail.

Community
  • 1
  • 1
Cedric Martin
  • 5,945
  • 4
  • 34
  • 66
  • 1
    See http://stackoverflow.com/questions/426258/how-do-i-check-a-checkbox-with-jquery-or-javascript – j08691 Mar 16 '12 at 21:08
  • @j08691: I tried this to no avail... I'm trying to change the checkbox from a callback called when one click precisely on that checkbox, can this be done? Aren't there "infinite events" issue? Where there's a change, triggers the call, I switch back the value, retriggers the call. – Cedric Martin Mar 16 '12 at 21:10
  • I think a change event is inevitable. If you want to avoid executing some code that occurs on change you will have to put in a catch. – Fresheyeball Mar 16 '12 at 21:15
  • So you want the callback for a change event on a checkbox to do the reverse of whatever the user's click did? The universe may implode if you get that working. – j08691 Mar 16 '12 at 21:16
  • @j08691: it's because actually any change does a post/redirect/get to a new page: each page is stateless and corresponds to one state (and one URL). It all works fine, besides for the fact that on some browsers when the user clicks on the back button he gets to a previous page, but with the checkbox wrongly {un}checked. If I could do what I'm asking here, I'd solve my problem... – Cedric Martin Mar 16 '12 at 21:23

2 Answers2

1

in change event add :

$(this).prop('checked', !this.checked);

Frenchi In LA
  • 3,139
  • 3
  • 25
  • 41
1

check my fiddle:

http://jsfiddle.net/QvaEA/

$(document).ready(function() {
  $('.cb').change(function(){
    if ($(this).is(':checked')) {
        alert('changed to checked -> uncheck again');
        $(this).attr('checked', false);
    } else {
        alert('changed to unchecked -> recheck');
        $(this).attr('checked', true);
    }
   });
});​

in callbackfunctions you should always use $(this) to access the triggering element. in your code, you would always check (and change) all checkboxes with the class cb.

StilgarBF
  • 1,060
  • 8
  • 17
  • +1, great jsfiddle there... However there's something that is totally confusing me: how comes that when you change the value, a new *change(...)* is not triggered? And is it guaranteed to never trigger in a future version of JQuery? – Cedric Martin Mar 16 '12 at 21:25
  • the change-event fires only on user-interactions. I don't know if thats a js/browser-limitation or done as a design-requirement in jQuery. You can be sure that its not triggered in any browser. At least that's what I read from the jQuery docu. – StilgarBF Mar 16 '12 at 21:36