[thelist] setAttribute(someevent) in IE?

liorean liorean at f2o.org
Tue Jun 1 13:16:48 CDT 2004


sbeam wrote:
> Folks:
> 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>
> ===

The DOM attribute handling really shouldn't be used for event handling. The DOM has another system for dealing with events. In your case, unless you're serving as XML, you should use the old DOM0 way of handling it:

    [object HTMLElement].onevent=functionreference;

In this specific case, 
    document.getElementById('thelink').onclick=foo;

In cases where you want to send it an argument,
    document.getElementById('thelink').onclick=function(e){
        e=e||window.event;
        /*code to run when the event triggers*/
    };

// David
-- 
David "liorean" Andersson

ViewStyles, ViewScripts, SwitchStyles and GraphicsInfo bookmarklets:
<http://liorean.web-graphics.com/>
Hangouts:
<http://codingforums.com/> <http://yourmusicforums.com/>


More information about the thelist mailing list