How to change the color of links with the user’s click?

Asked

Viewed 138 times

0

Hello! Well, I have a Topbar on my mini search site: http://isearch.baloon.url.ph, And now I’ve got a Javascript form action exchange system, which switches between "web, images, videos and news". However, the link "Web" is already selected (blue color), but I wanted when the user clicked on the other links, the clicked blue color and the others black color, highlighting the choice of research done. Thus follows the example of Babylon Search: http://search.babylon.com, that when we click on images, videos and news, the chosen option is highlighted.

  • But I wanted to do this without the user needing to leave the page they are on. Thank you!

  • Can you apply the "Selected" class by clicking on the link and deselecting the link that was clicked on? Ex: the one selected is the "web" link. When you click on images, the web deselects and the images selects. And whispers to the other links. You can do that without changing the page like Babylon?

1 answer

2

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: http://jsfiddle.net/62y1mvL9/

So this idea is divided into three parts:

  • select all the elements to click

  • define a function that executes in the click

And here in this function all the <li> to remove the class current and then add only that on, which clicked.

  • add the event receiver calling the function ativar when the element li receive a click

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

  • It worked in part. Look how it turned out: http://isearch.baloon.url.ph, I already had a Current class on the page, on the hot, super offers and top games, so the bass line went to the topbar as well. How do I make the topbars without lines and the tabs with lines? Go to: http://isearch.baloon.ur.ph Thank you very much!

  • @Gustavobarbosa: create a new class just for this menu and use it in the code I put on top.

  • Current already existed down there, then interfered in the links from above.

  • @Gustavobarbosa: create a new class just for this menu and use it in the code I put on top.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.