onclick reset class

Status
Not open for further replies.

louie

New Member
I have a javascript function which onclick changes the class style to li tag to active, but i am looking to reset the other li's if they are already set as active.

Code:
<script language="JavaScript">
function change(id, newClass) {
 identity=document.getElementById(id);
 identity.className=newClass;
}
</script>
<ul>
<li id="111" class="">
<a href="#" onclick="change('111', 'active');">link</a>
</li>
<li id="222" class="">
<a href="#" onclick="change('222', 'active');">link</a>
</li>
 
</ul>

I am looking for a solution to loop through the li tags and check the id that has been clicked and if <> then chabge class to nothing else change to active.
 

RedCardinal

New Member
Not sure if this will be any good to you but if I am tracking an event like this I set up a simple cache and record the last clicked element.
Code:
<script language="JavaScript">
var lastClicked = null;
function change(id, newClass) {
 identity=document.getElementById(id);
 if(lastClicked) {
  lastClicked.className = "";
 }
 identity.className=newClass;
 lastClicked = identity;
}
</script>
<ul>
<li id="111" class="">
<a href="#" onclick="change('111', 'active');">link</a>
</li>
<li id="222" class="">
<a href="#" onclick="change('222', 'active');">link</a>
</li>
 
</ul>

If multiple elements can be clicked then you would probably need a second function to cover multiple selected items and you would make lastClicked an array.

Hope it's some use.
 

louie

New Member
That worked like a dream. Thanks man. it was doing my head-in. I try to loop inside the li tags but i coudn't get it to work. My javascript is limited.

Seller - the page I am working on.
 

ph3n0m

New Member
uhm that function doesnt appear to be working in Firefox (assuming that when you click an option in the left menu, that options becomes "active" and de-activates the home or previously selected option)
 

louie

New Member
It worked, but I made few changes this morning due to lack of browser history (because of Ajax) and the left menu actually gets refreshed, the reason it doesn't stay active. I am working on it.
 

RedCardinal

New Member
uhm that function doesnt appear to be working in Firefox (assuming that when you click an option in the left menu, that options becomes "active" and de-activates the home or previously selected option)

Sorry - I was just giving an example of how you could go about. Never tested or otherwise on any platform.

Hope you get it working Louie, but if you have any problem give me a shout and I will take a look at the actual script.
 
Status
Not open for further replies.
Top