Using jQuery, I set a link tag’s click handler like this:
$('#lnk').click(handleClick);
handleClick does something like this:
function handleClick() {
var $this = $(this);
...
}
Now I have a need to directly call handleClick() outside of #lnk being clicked. Is there a way for me to set this when calling handleClick?
You can use
apply()orcall():But of course
theElementThatShouldBeThismust be a DOM element or something that is accepted as input forjQuery().And it becomes more tricky if you are accessing the
eventelement insidehandleClick. But I will just assume you don’t 😉You can also always execute the event handler in the context of
#lnkby calling$('#lnk').click()(which simulates a click).