In the div with id="menu" i have some <a> tags
I want when I click in one <a> tag of this div all <a> tags of this div get className="inactive" but especially the tag i have clicked to get className="active"
So I made this!
Code:
<html>
<head>
<script type="text/javascript">
function changemenu(obj) {
var m = obj.getElementsByTagName("A");
for (i=0; i<m.length; i++) {
m[i].className="inactive";
if ()
m[i].className="active";
};};
</script>
<body>
<div onclick="changmenu(this);">
<a>Bla Bla Bla</a> <a>Bla Bla Bla</a> <a>Bla Bla Bla</a>
</div>
</body>
</html>
Now the only thing i search is what will be the case of if statement in changemenu function.
Thanks in advance!
That's precisely your problem - the way you've done it, it's going to be very hard to know which link was clicked, i.e. which link to give the class 'active', not 'inactive'. Much easier with events (even easier with jQuery but I don't know if you're proficient in that).
Code:
window.onload = function() {
var divObj = document.getElementById('menu');
var links = divObj.getElementsByTagName('a');
window.addEventListener ? divObj.addEventListener('click', changeClasses, false) : divObj.attachEvent('onclick', changeClasses);
function changeClasses(e) {
var trigger = e.target ? e.target : window.event.srcElement;
var counter = 0;
var link;
while (link = links[counter]) {
link.className = link != trigger ? 'inactive' : 'active';
counter++;
}
}
}
Bookmarks