[thelist] setAttribute(someevent) in IE?

Joshua Olson joshua at waetech.com
Tue Jun 1 12:26:09 CDT 2004


> -----Original Message-----
> From: sbeam
> Sent: Tuesday, June 01, 2004 1:05 PM
>
> the following code works as expected in Moz but does nothing in Opera
> and IE. Anyone know how to acheive this?
> ===
> <a id="thelink" href="#">Click Me</a>
> <script type="text/javascript">
>     document.getElementById('thelink').setAttribute('onclick', 'foo()');
>
>     function foo() {
>         alert('foo');
>     }
> </script>

sbeam,

setAttribute shouldn't be used to set event handlers--instead, try using
something much simpler:

document.getElementById('thelink').onclick = foo;

Remember, setAttribute sets the attribute to the value of the second
argument, which is a string.  So, setting the onclick attribute to the
string "foo()" is probably not what you want in most cases.

Best of luck,

<><><><><><><><><><>
Joshua Olson
Web Application Engineer
WAE Tech Inc.
http://www.waetech.com/service_areas/
706.210.0168





More information about the thelist mailing list