When clicking on each div it should alert '1' if div 1 was clicked on or '5' if div 2 was clicked on. I have tried to make this code as easy to as possible because this is needed in a much larger application.
<html>
<head>
<style type="text/css">
#div1 { background-color: #00ff00; margin: 10px; padding: 10px; }
#div2 { background-color: #0000ff; margin: 10px; padding: 10px; }
</style>
<script type="text/javascript">
function init()
{
var total = 1;
var div1 = document.getElementById('div1'),
div2 = document.getElementById('div2');
var helper = function(event, id)
{
if (event.stopPropagation) event.stopPropagation();
if (event.preventDefault) event.preventDefault();
alert('id='+id);
}
div1.addEventListener('click', function(event) { helper(event, total); }, false);
total += 4;
div2.addEventListener('click', function(event) { helper(event, total); }, false);
}
</script>
</head>
<body onload="init();">
<div id="div1">1</div>
<div id="div2">2</div>
</body>
</html>
Thanks for your help! :-)