Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  31] [ 6]  / answers: 1 / hits: 77945  / 14 Years ago, tue, april 13, 2010, 12:00:00

This seems deceptively simple.



How do you override the onclick event in the following HTML using JavaScript?



<a id=sample href=... onclick=alert('hello world') />...</a>


I've tried it with jQuery, but that didn't do the trick:



$('#sample').attr('onclick','alert(done)')


Assume the HTML is pre-written and cannot be changed, other than manipulating it with JavaScript.


More From » html

 Answers
22

Try this:



// Setting the DOM element's onclick to null removes 
// the inline click handler
$(#sample)[0].onclick = null;
$(#sample).click(function() { alert(done) });

[#97086] Saturday, April 10, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marcelofrankiea

Total Points: 200
Total Questions: 96
Total Answers: 101

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;