Pregunta

I have a menu in which different buttons are placed. When the initial page is loaded, one button is active (declared via class="active). Now, when I click on a different button, the class will be removed and added to the just clicked button. The code worked well, then I tried something different in jquery but I removed it, but my code isn't working, even though I have a different project in which I have the same code and it's working.

Here's my Jsfiddle:

http://jsfiddle.net/3e2MW/1/

¿Fue útil?

Solución

You are using $(this) to bind the click event handler which is wrong.Use this:

$('#nav ul li a').click(function() {
           $("#nav ul li a").removeClass("active");
           $(this).addClass('active');
           return false;});

Working Demo

Otros consejos

change the code like this:

$(document).ready(function() {
$("#nav ul li a").click(function(e) {
           e.preventDefault();
           $("#nav ul li a").removeClass("active");
           $(this).addClass('active');  
           return false;
         });
 });

Demo

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top