[thelist] setAttribute(someevent) in IE?

sbeam sbeam at syxyz.net
Tue Jun 1 13:20:21 CDT 2004


On Tuesday 01 June 2004 01:26 pm, Joshua Olson wrote:
> 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.

actually the DOM2 way to do it is with addEventListener() but IE has 
never heard of that. 

So how do we pass an argument to foo()? I can't seem to find a 
definitive answer on this. Trying stuff now like:

==
<a id="thelink" href="#">Click ME</a>

<script type="text/javascript">
var op = 'somevalue';

elem = document.getElementById('thelink');
elem.setAttribute('opAttrib', op);
elem['onclick'] = foo;

function foo(e) {
    alert('foo:'+e.target.getAttribute('opAttrib'));
}
</script>
==

which again works fine in Moz and Opera but IE doesn't have an e.target 
(or e.srcElement, which is what microsoft suggests in the KB) - anyone 
have a good way of doing this?

thanks


-- 

# S Beam - Web App Dev Servs
# http://www.onsetcorps.net/


More information about the thelist mailing list