tags:

views:

33

answers:

2

I have a series of link on my pages with random a:hover classes so they display different colors on mouse over.

When a link is clicked its set to a different class so that it looks highlighted or active.

I need to create a script that first saves the class of the clicked link and then after it is changed and a new link is clicked finds the highlighted element and changes it back to the original class it was before changing the new active link.

What is the best way to store the class of the link until the new link is called.

Something like:

Get the class of the previous highlighted link from a variable? Set the previous link to its original class.

Store the current class of the new link. Set the element as the current highlighted link.

A: 
 var old_link = null;
 function store(ele){
   old_link = {
     cn: ele.className,
     r: ele
   }
 }
 function restore(){
   old_link.r.className = old_link.cn;
 }
scragar
What do cn: and r: do?
ian
`cn` is the class name before editing.`r` is a reference to the element.I just wrote it like that so a single variable could be used to store both pieces of information.
scragar
A: 

I'd do this slightly differently.

For any links that you want included, they should all have the same class, let's say 'link-class'.

When you click on a particular link you'll want to add the class of 'selected' to that link, but first you'll want to remove the 'selected' class from all links on the page with the class of 'link-class'.

In jquery it would look like

$('a.link-class').click(function() {
    $('a.link-class').removeClass('selected');
    $(this).addClass('selected');
});
Bela