Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  195] [ 4]  / answers: 1 / hits: 158158  / 15 Years ago, fri, june 19, 2009, 12:00:00

My goal is to change the onclick attribute of a link. I can do it successfully, but the resulting link doesn't work in ie8. It does work in ff3.



For example, this works in Firefox 3, but not IE8. Why?



<p><a id=bar href=# onclick=temp()>click me</a></p>

<script>
doIt = function() {
alert('hello world!');
}
foo = document.getElementById(bar);
foo.setAttribute(onclick,javascript:doIt(););
</script>

More From » onclick

 Answers
17

You don't need to use setAttribute for that - This code works (IE8 also)



<div id=something >Hello</div>
<script type=text/javascript >
(function() {
document.getElementById(something).onclick = function() {
alert('hello');
};
})();
</script>

[#99280] Monday, June 15, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
collinisaaka

Total Points: 194
Total Questions: 105
Total Answers: 104

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