Forum Moderators: open

Message Too Old, No Replies

Add function to element.

Possible?

         

Dabrowski

7:26 pm on May 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to add a function onto an element. I know you can add any property by using setAttribute, but not sure about functions...

For example....

var div = document.getElementById( "test");

div.myFunction = function { alert( "This is my div!"); };

//
// Then later.....
//

div.myFunction();

Possible? Anybody done this?

StupidScript

8:24 pm on May 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For example:

<div id="test" onmouseover="ddiv.myFunction('somediv')">TEST</div>
<script type="text/javascript">
var ddiv=document.getElementById("test");
ddiv.myFunction = function (txt) { alert("This is a DIV: "+txt); }
</script>

[edited by: StupidScript at 8:27 pm (utc) on May 10, 2007]

Dabrowski

10:04 pm on May 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, that works in the big 3. I really should have tried it before asking!

Thanks.

Drag_Racer

8:29 am on May 12, 2007 (gmt 0)

10+ Year Member



its as simple as you think Dabrowski

document.getElementById('myObject').onclick=function(){alert('here I am')};