How to do color toggling of links with the click of the user?

0

Hello! Well, I have a TopBar on my mini search site: link , and now I've got a form action swapping system with Javascript , which toggles between "web, images, videos and news". However, the "Web" link is already selected (blue), but wanted that when the user clicks on the other links, the click is blue and the others are black, highlighting the search choice made. So follow the example of Babylon Search: link , that when we click on images, videos and news, the chosen option is highlighted.

    
asked by anonymous 04.10.2014 / 17:18

1 answer

3
var links = document.querySelectorAll('ul.tabs-title li');

function ativar() {
    for (var i = 0; i < links.length; i++) {
        links[i].classList.remove('current');
    }
    this.classList.add('current');
}

for (var i = 0; i < links.length; i++) {
    links[i].addEventListener('click', ativar);
}

Example: link

So this idea is divided into 3 parts:

  • Select all elements to click

  • Define a function that executes in the click

And here in this function all <li> is moved to remove the current class and then add only the one in which you clicked.

  • add the event dropper that calls the ativar function when the li element receives a click

I used the HTML in the link that you put in the question.

    
04.10.2014 / 17:42