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
When
manage(val1, val2)is called,thiswill be the global object (or undefined if you’re in strict mode)But you can set the
thisvalue manually withcall. Try this:Now
thiswill be whatever you just clicked on.Just to clarify a bit more, inside the
onclick="____"thisis the thinig you clicked on. But once you call functions from there, this becomes the global object in the functions. So: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.