How to remove all classes except the one you clicked?
Asked Answered
V

6

6

This functions starts when I click on a link. It needs to remove all '.is-active' classes on the elements with the attribute [data-route]. And add the class '.is-active' on the [data-route] element that is connected with the link I clicked on.

    toggle: function(section){
        var sections = document.querySelectorAll('[data-route]');
        for (i = 0; i < sections.length; i++){
            document.querySelector('[data-route]').classList.remove('is-active');
        }
        document.querySelector(section).classList.add('is-active');
    }

But this doesn't work. It doesn't remove the classes?

See example: http://jordypouw.github.io/myFED2/deeltoets1/index.html

P.S. it has to be in vanilla JavaScript.

Vibratory answered 23/9, 2014 at 19:33 Comment(1)
you must loop on all document.querySelector('[data-route]')... you need to store that in a variable and loop them. Also you must use querySelectorAllFolk
I
13
toggle: function(section){
    var sections = document.querySelectorAll('[data-route]');
    for (i = 0; i < sections.length; i++){

        sections[i].classList.remove('is-active');

        // querySelectorAll return an array of dom elements, u can access them directly.

    }

    // I suppose in your case that ' section ' variable is the clicked element so :

    section.classList.add('is-active')

   // if not you have to store the dom element from the event, and add the class here.

}
Ial answered 23/9, 2014 at 19:43 Comment(2)
Can't we do this without the loop? like in Jquery we do it.Duchy
@TahirAfridi document.body.querySelectorAll('[data-route]').forEach(i => i.classList.remove('is-active')) You still need a loop, but its more neatTearle
F
2

you can do this:

for (var item of document.querySelectorAll('[data-route]')) {
    item.classList.remove('is-active');
}

This is ecmascript6 so it won't work on old browsers. I like it because it's clean and nice. to get it to work on other browsers you must convert the nodes collection into a real array, so you could loop it.

Folk answered 23/9, 2014 at 19:39 Comment(3)
Ah yes. I think this guy is right. Forgot that there was a loop being used.Tumultuous
Cool, is this the short version of Samsy his solution?Vibratory
yes, his answer is most straight-forward way of doing this, but not the coolest :)Folk
A
1
toggle: function(section){
    document.querySelectorAll("[data-route]").forEach( e => {

        e.classList.remove("is-active");
    });
    // querySelectorAll return an array of dom elements, u can access them directly.

    // I suppose in your case that ' section ' variable is the clicked element so :

    document.querySelectorAll("[data-route]").forEach( e => {

        e.classList.add("is-active");
    });

   // if not you have to store the dom element from the event, and add the class here.

}
Appoint answered 16/3, 2020 at 6:24 Comment(0)
M
0

Set up a variable for the clicked item..

jQuery('.clicker-item').on("click", function(){

var clicked = jQuery('.clicker-item').not(jQuery(this));

clicked.removeClass("active")
jQuery(this).toggleClass("active")


});
Mcmillon answered 19/2, 2016 at 19:22 Comment(0)
F
0

I felt, that other answers were not neat enough.

toggle: (s) => {

    // Find all other sections and remove the active class:
    document.body.querySelectorAll('[data-route]').forEach(i => i.classList.remove('is-active'))
    
    // Add active to the inputted section:
    s.classList.add('is-active')
}
Fourflush answered 28/12, 2020 at 18:31 Comment(0)
T
-2

shouldn't it be this:

toggle: function(section){
    var sections = document.querySelectorAll('[data-route]');
    for (i = 0; i < sections.length; i++){
        document.querySelector('[data-route]').removeClass('is-active');
    }
    document.querySelector(section).addClass('is-active');
}

Edit: Sorry, I should have said removeClass and addClass

Tumultuous answered 23/9, 2014 at 19:37 Comment(3)
your answer makes no sense, you are looping sections but don't do anything with it inside the loop.Folk
yeah, see my reply under your answer. I overlooked that he was doing a loopTumultuous
so you can edit your answer or remove it..cause it makes no sense leaving an answer that doesn't work.Folk

© 2022 - 2024 — McMap. All rights reserved.