Jump to content

Set an event function from a string


shadowayex

Recommended Posts

I have a function where a user will pass in a string representation of an event (such as "onclick" or "onmouseover") and I want to set a function to that event. I was wondering if, rather than doing a long if statement, there was a way to do so using the string.

Link to comment
Share on other sites

You can just use array notation:

var event_str = "onclick";document.getElementById("element")[event_str] = function() {	do_something();}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...