RSS
 

Search results for ‘dynamically add event handler javascript’

Javascript: Dynamically Change onclick Event

05 Jul

To dynamically change or add an “onclick” event handler to an element, try:

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;
}
VN:F [1.9.4_1102]
Rating: 6.8/10 (164 votes cast)
VN:F [1.9.4_1102]
Rating: +14 (from 50 votes)
Retweet