How to add an event to an element in javascript
By liviu - Posted on May 17th, 2009
Tagged:
You can add events in javascript to elements. I don't know if i ever saw this used but it can be very useful. I've seen this done a lot in actionscript.
To add an event to an element you get he element first in a variable (usually with document.getElementById(id) ) and then use the method addEventListener.
Here is an example:
<a href="#" id="link1">click here</a>
<script>
element1 = document.getElementById("link1");
element1.addEventListener('click',doSomething2,true)
function doSomething2()
{
alert('The link was clicked and something was done');
}
</script>
Bookmark/Search this post with:
- liviu's blog
- Login or register to post comments
Delicious
Digg
StumbleUpon
Propeller
Reddit
Magnoliacom
Newsvine
Furl
Facebook
Google
Yahoo
Technorati
Icerocket