The way I know:
HTML:
<input type="button" id="msg" onclick="foo(this);" />
JS:
function foo(elementObj) { elementObj.toggle(); }
Is there any way to get reference to elementObj inside foo() without passing it as function argument?
How I want to do:
HTML:
<input type="button" id="msg" onclick="foo();" />
JS:
function foo() { elementObj = <any way I can get ref to html element here?>; elementObj.toggle(); }
P.S.
Call function like this: $("#msg").click(function(elementObj ) {elementObj.toggle();}
Is not what I need.
Yes, just use proper unobtrusive jQuery event handler attaching such as
.on, or its shorthands:Or if you need to use the function
fooelsewhere..As per OP edit:
Yeah just use the ID selector
$('#msg')or$(this)if you want to reference the clicked element.