0

i have an html page where i have multiple "checkboxes" each of them are call a function onclick.

Something like this

onclick="manage(val1, val2)

And in my function when i am getting the check status of checkbox, giving false every time.

function manage(val1, val2) {
    **if ($(this).is(":checked")) {** //returns false everytime
        //do something
        //
    }
    else {
        //do something
    }
}

please tell me where i am doing mistake...

Thanks in advance

3 Answers 3

5

When manage(val1, val2) is called, this will be the global object (or undefined if you're in strict mode)

But you can set the this value manually with call. Try this:

onclick="manage.call(this, val1, val2);"

Now this will be whatever you just clicked on.

Just to clarify a bit more, inside the onclick="____" this is the thinig you clicked on. But once you call functions from there, this becomes the global object in the functions. So:

onclick="foo(this);"

function foo(val) {
    alert(this);
    alert(val);
}

Alerts [object DomWindow] (the global object) then [object HtmlInputElement] (the thing I clicked on)

Without getting into too much detail, this is a result of how "function invocation" works in JavaScript.

9
  • I could have sworn browsers did that automatically... Edit: Or maybe I'm thinking about when things are done like onclick="blah(this)" :)
    – Corbin
    Commented Dec 23, 2011 at 1:30
  • +1 for using call instead of just passing as a regular argument. Brings it into conformity with the expectation.
    – user1106925
    Commented Dec 23, 2011 at 1:33
  • 1
    @ЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖ - :) (thanks to your username I never have to worry about having too short of a comment) Commented Dec 23, 2011 at 1:37
  • @ЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖ - and yeah, call and apply are some of my favorite tools to use in JavaScript Commented Dec 23, 2011 at 1:38
  • 1
    That is quite a beast of a name. And I think that call and apply are quite integral to the design of JavaScript :p.
    – Corbin
    Commented Dec 23, 2011 at 5:12
5
$('input[type="checkbox"]').click(function(){  // append click to any checkbox
    if ($(this).is(':checked')) {
        // do something
    }
});
4
  • Just some suggestions, there is the :checkbox selector and this.checked. Your selector can take advantage of QSA without any pre-processing though :)
    – alex
    Commented Dec 23, 2011 at 1:43
  • @alex: [type="checkbox"] is qSA friendly, unless I misunderstood you?
    – user1106925
    Commented Dec 23, 2011 at 1:54
  • @amnotiam: I think you misunderstood me :)
    – alex
    Commented Dec 23, 2011 at 2:13
  • @alex: Ah yes, very likely. I thought you were saying [type="checkbox"] would prevent qSA. Totally with you on using this.checked though. :)
    – user1106925
    Commented Dec 23, 2011 at 2:15
2

You'll need to set the context of your onclick, or pass it 'this' as a parameter:

<input type="checkbox" onclick="manage(this, 'val1', 'val2');"></input>
<script>
function manage(that, val1, val2) {
   if ($(that).is(":checked")) {
       alert('checked')
    }
    else {
       alert('unchecked')
    }
}
</script>

jsFiddle

Of course your best bet is to NOT use in-line javascript, go with something like TerryR wrote.

Not the answer you're looking for? Browse other questions tagged or ask your own question.