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.

Sign up to request clarification or add additional context in comments.

9 Comments

I could have sworn browsers did that automatically... Edit: Or maybe I'm thinking about when things are done like onclick="blah(this)" :)
+1 for using call instead of just passing as a regular argument. Brings it into conformity with the expectation.
@ЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖ - :) (thanks to your username I never have to worry about having too short of a comment)
@ЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖ - and yeah, call and apply are some of my favorite tools to use in JavaScript
That is quite a beast of a name. And I think that call and apply are quite integral to the design of JavaScript :p.
|
5
$('input[type="checkbox"]').click(function(){  // append click to any checkbox
    if ($(this).is(':checked')) {
        // do something
    }
});

4 Comments

Just some suggestions, there is the :checkbox selector and this.checked. Your selector can take advantage of QSA without any pre-processing though :)
@alex: [type="checkbox"] is qSA friendly, unless I misunderstood you?
@amnotiam: I think you misunderstood me :)
@alex: Ah yes, very likely. I thought you were saying [type="checkbox"] would prevent qSA. Totally with you on using this.checked though. :)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.