addClass and removeClass with Jquery

Asked

Viewed 15,429 times

5

I am trying to create a menu where a div wins a class if clicked, but I would like to remove the class from the last link clicked in the menu.

<div id="bolinha-dentro"></div><a href="">Meu link</a>
<div id="bolinha-dentro"></div><a href="">Meu link</a>
<div id="bolinha-dentro"></div><a href="">Meu link</a>
<div id="bolinha-dentro"></div><a href="">Meu link</a>

I want the active class to be added on the link I click and in case I click on the next menu link it will remove the active class and add on another.

$(document).ready(function() {
    $('#bolinha-dentro').click(function(){
        $(this).toggleClass('active');     
    });
})

Link to the fiddle this one

  • 1

    the value of the id attribute must be unique in the document: http://www.w3.org/TR/html401/struct/global.html#h-7.5.2

  • Did you manage to solve this problem? Did any of the questions help to solve?

4 answers

3

I considered the following to answer

  • the Ids were duplicated
  • the classes were repeating, I put everything in one parent item, with only one class
  • moved the links inside the div, because they were empty and could not be clicked

$(function(){ // equivalente a $(document).ready(function(){
  $('.bolinhas a').click(function(event) {
    event.preventDefault();
    $('.bolinhas > div').removeClass('active');
    $(this).parent().addClass('active');
  });
});
.bolinhas > div.active {
  background-color: #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="bolinhas">
  <div><a href="#">Meu link</a></div>
  <div><a href="#">Meu link</a></div>
  <div><a href="#">Meu link</a></div>
  <div><a href="#">Meu link</a></div>
</div>

  • Sanction my code is here https://jsfiddle.net/bruno/AzDQm/4/

1

$(function() {
$('.bolinha-dentro').click(function(){
  $('.bolinha-dentro').removeClass('ativo');
  $(this).addClass('ativo');     
});
});
.bolinha-dentro{ background:#000}
.bolinha-dentro a,.bolinha-dentro a:visited{text-decoration:none; color:#FFF}
.ativo{ background:#FF0000; color:#FFF }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bolinha-dentro"><a href="#" >Meu link</a></div>
<div class="bolinha-dentro"><a href="#">Meu link</a></div>
<div class="bolinha-dentro"><a href="#">Meu link</a></div>
<div class="bolinha-dentro"><a href="#">Meu link</a></div>

  • Jefferson, I’d like to add an active class to the "ball-in" class div and make that exchange effect. http://jsfiddle.net/bruno/AzDQm/4/

  • I changed the code. abcs

0

<div class="bolinha-dentro></div><a href="">Meu link</a>
<div class="bolinha-dentro></div><a href="">Meu link</a>
<div class="bolinha-dentro></div><a href="">Meu link</a>
<div class="bolinha-dentro></div><a href="">Meu link</a>

First thing, if you want to call the click event, #represents ID and class..

$(document).ready(function() {
    $('.bolinha-dentro').click(function(){
        $(this).toggleClass('active');     
    });
})

That should work.

  • I got confused a little bit here, it’s actually ID, is that I typed the code instead of copy and paste.

  • And yet it doesn’t work?

0


You need to use classes in ID view. Ids are unique, classes are for repeated elements that share something in common.

Also note that your HTML is badly formatted because you have li starting within anchors that close off from them...

<div id="bolinha-fora"></div><li> Contato </a></li>   // errado

Having said that you can do it with Javascript like this:

(function () {
    var links = document.querySelectorAll('#nav-menu ul li');
    [].forEach.call(links, function (li) {
        li.addEventListener('click', mudarClasses);
    });

    function mudarClasses() {
        [].forEach.call(links, function (li) {
            li.classList.remove('ativo');
        });
        this.classList.add('ativo');
    }
})();

jsFiddle: http://jsfiddle.net/hcwzmvLn/

Browser other questions tagged

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