tags:

views:

82

answers:

2

Hello

I want the checkbox to be checked when I am using jquery toggle function. It works fine when I use the .bind('click').

$('#chkb').toggle(function () {

    $('#va').text("checked");
    $('#chkb').attr('checked', 'checked');

}, 
function () {

    $('#chkb').attr('checked', 'false');
    $('#va').text("");

});

html

<input type="checkbox" id="chkb" />

How do I get it done.

Thanks Jean

A: 

do you really have to checked it in code? (Is there something I missed? checkbox when clicked, already toggle it's checked attribute value.)

Better way to do this is by using change like this,

$('#chkb').change(function () {
    var self = this;
    $('#va').text(function(i,text){
        return self.checked?"checked":"";
    });
})
Reigel
You can check a checkbox with the keyboard, too.
ZeissS
ahh... so better to use `.change()` then. ;)
Reigel
What about this var apchk = '<input type=checkbox checked="checked" id="chkb">'; $('#c').html(apchk);
Jean
@Jean - Why? what's with that?
Reigel
@reigel well that is one way I just found out before I checked out the solutions here
Jean
@Jean - and that did the trick you need? Cause honestly, I don't get that. ^_^
Reigel
I don't get the "var apchk" thing either, this solution looks up '#va' each change, better to stick it in a variable ... ?
NimChimpsky
Yes, it worked. now I have another problem, $('#va').text(""); does not work. I put the checkbox between <div></div> and did a .html().
Jean
Your first code doesn't work (in Chrome), and I'm not sure why not. ==> http://jsfiddle.net/9YShm/ ----- The lettering changes, but it always looks unchecked. ----- It works with change.
Peter Ajtai
@Jean - Why don't you edit the HTML into the question? (I mean included the `<div></div>` too.
Peter Ajtai
@Peter - I've never tested it as to it looks right. But now, I will ask SO, don't really know the cause of it.
Reigel
With the .html() it works in chrome. The code in the question does not work in chrome ...
Jean
what's with the down-vote? could anyone explain to be fair?
Reigel
cos the code didn't work
NimChimpsky
@NimChimpsky - the erased one did not work. IMHO, it is rude just down-voting without giving the poster to explain first it's view.
Reigel
sorry but it wasn't erased when I clicked...
NimChimpsky
Why did someone down vote this answer?
Jean
you posted code that didn't work - whats the big deal ? fine its erased now, but it wasn't at the time, it was presented as working valid code. If thats not worth a down vote I don't know what is.
NimChimpsky
@Nim - haha nevermind. Arguing like this won't help Jean at all.
Reigel
Lol. True. TBH I would have thought the erased code would have worked, , but I need me some points too ... :-) jQuery is great, saves me so much time.
NimChimpsky
Its interesting though, the code in the question does not work!
Jean
@Jean - what did not work? - [this code](http://jsfiddle.net/7bLG7/)?
Reigel
@Jean - about `.toggle()` on checkbox, [read this](http://stackoverflow.com/questions/3837844/checkbox-with-toggle)
Reigel
+2  A: 
chkb = $('#chkb');
va = $('#va');

chkb.change(function () {
   va.text(chkb[0].checked ? "checked" : "");
});

Caching selectors into variables speeds things up - doesn't have to search the dom each time. Putting chkb into a variable means it can be used elsewhere in your jQuery, instead of using this.

NimChimpsky
+1 ^_^ one comment though. Although jQuery helps you a lot, don't abuse it. `chkb[0].checked` is faster than `chkb.is(':checked')`. cheers!
Reigel
@reigel, thanks ok will edit
NimChimpsky
you mean [this code](http://jsfiddle.net/VJj4R/) did not work on you?
Reigel
lol, we all make mistakes :-)
NimChimpsky
@Nim - yeah and a down-vote won't help. ;) Just some explanation and all of us are happy. ;)
Reigel