Link to home
Start Free TrialLog in
Avatar of wrynn
wrynn

asked on

attaching and detaching onclick=somefunction(this) to a div dynamically

how can i attach onclick="someFunction(this)" to a div through javascript?  how can i de attach it?  thanks
Avatar of Jason Minton
Jason Minton
Flag of United States of America image

document.getElementById('divID').onclick = someFunction();
another example:

var el = document.getElementById('foo');
el.onclick = showPopup;
//NOTE: showPopup();
//or showPopup(param);
//will NOT work here.
//Must be a reference to a function,
//not a function call.

function showPopup() {
  var popup = window.open(this.href, "popup", "height=800,width=600");
  popup.focus();
  return false;
}
Avatar of wrynn
wrynn

ASKER

but how can i do el.onclick = showPopup(this)  
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial