Skip to content

Instantly share code, notes, and snippets.

@leonelag
Created April 25, 2012 11:50
Show Gist options
  • Save leonelag/2489155 to your computer and use it in GitHub Desktop.
Save leonelag/2489155 to your computer and use it in GitHub Desktop.
Example of how to attach an event to HTML elements
<html>
<head>
<style>
li { border: thin solid red }
</style>
</head>
<body>
<ul>
<li id="my-elem-1">1</li>
<li id="my-elem-2">2</li>
<li id="my-elem-3">3</li>
<li id="my-elem-4">4</li>
<li id="my-elem-5">5</li>
<li id="my-elem-6">6</li>
<li id="my-elem-7">7</li>
<li id="my-elem-8">8</li>
</ul>
<script type="text/javascript">
var items = document.getElementsByTagName("li")
for (var i = 0; i < items.length; i++) {
var ii = items.item(i);
ii.onmouseover = function() { alert(this.id); }
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment