PDA

View Full Version : last item clicked



Navarone
August 5th, 2008, 10:21 AM
I have a menu and I need to know the last menu item clicked. So if I click on menu item 1 and then click on menu item 2, I need to know that menu item 1 was the last item clicked. Is there a simple javascript function that will do this? :|

jwilliam
August 5th, 2008, 04:32 PM
Hey Navarone... sorry I've been a little distracted lately. You're menu is looking good. Here's a sample script I threw together to achieve what you're looking for (I only tested in FF2):




<html>
<head>
<script type="text/javascript" language="javascript">
<!--
var last_item_clicked;
window.onload = function()
{

var list = document.getElementById('my_list');
var lis = list.getElementsByTagName('li');
for(var i in lis) {
if(typeof lis[i] == 'object') {
addEvent(lis[i].childNodes[0], 'click', function(e)
{
if(e == null) {
e = window.event;
}
var target = (e.target != null) ? e.target : e.srcElement;
if(last_item_clicked) {
alert("Last item clicked: " + last_item_clicked.id);
}
last_item_clicked = target;
}, true);
}
}

}

function addEvent(o, type, func, prop)
{
if(!o) {
return;
}
if(!document.addEventListener && document.attachEvent) {
o.attachEvent("on" + type, func);
} else {
o.addEventListener(type, func, prop);
}
}

//-->
</script>
</head>
<body>
<ul id="my_list">
<li><a href="#1" id="link1">Link 1</a></li>
<li><a href="#1" id="link2">Link 2</a></li>
<li><a href="#1" id="link3">Link 3</a></li>
<li><a href="#1" id="link4">Link 4</a></li>
<li><a href="#1" id="link5">Link 5</a></li>
<li><a href="#1" id="link6">Link 6</a></li>
</ul>
</body>
</html>

Navarone
August 5th, 2008, 05:21 PM
thanks jwilliam,
I already came up with my own solution but I greatly appreciate your reply. Thanks for the complements on the menu.