Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding an event to a tag

Status
Not open for further replies.

stinkybee

Programmer
May 15, 2001
218
GB
Is there any way of dynamically adding or changing an event for an HTML tag.

I would like to add or change an 'onMouseOver' event within a <div> tag (or any other tag for that matter).

I have tried 'setAttribute' but this does not work, presumably because it is an event not an attribute that I want to set.



 
setAttribute() only works in Netscape...try attachEvent('onmouseover',name of function); instead! However, it is not possible to call a function which expects any parameters (attachEvent('onmouseover',foo(x, y)); would fail!).

Hope this helps!
 
to set events use the following (these are for mouseMove)...
for ie:
document.onmousemove = someFunction;
for ns < 6:
document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = someFunction;
for ns6:
document.addEventListener(&quot;mousemove&quot;, someFunction, true);

this is basically the 3 ways to set events for the broswers (ie,ns<6, and ns6)
 
Thanks for the answers, I have had some success with both.
Only problem is the inability to pass parameters.

Oh well, you can't have it all.
 
You can grab the parameter from the arguments collection, form inside the function.


function someFunction(){

alert(&quot;Hello &quot; + arguments[0]);

}

someFunction(&quot;Gary Busey&quot;);

yo [bb]

P.S. setAttribute does work for IE5.
 
here, i wrote some basics: faq216-1063 but it is not in DOM & so, i don't know would it be of any help

there i used handler=new Function(&quot;body of the function&quot;)

but here is another way:
handler=new function(){
//put here whatever you want:
callSomeFunction(with, parametres)
}


something like that..

 
Thanks, the 'new Function' method works a treat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top