shadowayex Posted January 25, 2011 Share Posted January 25, 2011 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 More sharing options...
Synook Posted January 25, 2011 Share Posted January 25, 2011 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 More sharing options...
shadowayex Posted January 26, 2011 Author Share Posted January 26, 2011 Oh that's nifty. Thanks =) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.