• Geek Answers Handbook

    Removing a class from Div in Javascript

    I have a div section

    <div class="1"> <div id="tab1""> <ul class="nav nav-tabs"> <li class="active ans-tab"> <a href="{$cdn2}">MyText</a></li> <li class="topTab"><a href="{$cdn2}/Game/">Game</a></li> <li class="topTab"><a href="{$cdn2}/lb/">LB</a></li> </ul> </div> <div id="tab2"> <ul class="nav nav-pills"> <li class="active"> <a href="{$cdn2}">Top</a></li> <li><a href="{$cdn2}/categories/">Categories</a></li> </ul> </div> </div> 

    What I'm trying to do on every page used as an href in a div is to remove the class attribute from the li element that has it as active, and assign it to one, click.

    I tried removeClass removeAttribute etc. but nothing works for me. I want to use simple javascript no jQuery

    For example, my JS code on the game page removes the active class from the MyText page and adds it to the game page element.

    +4
    javascript
    Codemonkey Apr 22 '13 at 18:55
    source share
    3 answers

    I assume that only one is active at a time. To remove, you can do this:

     var active = document.querySelector(".active"); active.classList.remove("active"); 

    Then, to add, do the following:

     // Assuming `this` is your element this.classList.add("active"); 

    Both of them will work in modern browsers. For older browsers, use the same DOM selection, then perform typical manipulations with the .className string.

     active.className = active.className.replace(/\bactive\b/, " "); this.className += " active"; 
    +15
    silly little me Apr 22 '13 at 19:02
    source share

    Try the function to remove the class:

     function removeClass (parentEl, classToBeRemoved) { // DOM element, className for remove. var classes = parentEl.className.split(" "); for (var i = 0; i < classes.length; i++) { if (classes[i] == classToBeRemoved) { // Remove class. classes.splice(i, 1); } break; } parentEl.className = classes.join(" "); } 
    0
    Mihail Apr 22 '13 at 19:02
    source share

    Like this

     .bold { font-weight: bold } .red { background: red } <div id="theDiv" class="bold red">Bold and red</div> <button id="button">Remove red</button> var theDiv = document.getElementById("theDiv"); var button = document.getElementById("button"); function removeRed() { var classContent = theDiv.className; theDiv.className = classContent.replace("red", "").trim(); } button.addEventListener("click", removeRed, false); 

    on jsfiddle

    In new browsers you can use element.classList and remove method

    0
    Xotic750 Apr 22 '13 at 19:14
    source share

    More articles:

    • How to read gzip file in f #? - f #
    • MySql gets a list of unique words from a table where the values ​​in the field are separated by a comma - sql
    • Wrap end-of-line code vim - vim
    • Is it possible for the constructor to accept a series of parameters defined by the previous parameter? - java
    • using ember-rails rail handle template out of context of ember view - ember.js
    • How to remove a class in javascript? - javascript
    • In Java, how to navigate two lists at the same time? - java
    • to delete an element if it has a certain string - python
    • Access to a resource calendar without a mailbox through EWS and C # - c #
    • Rails config.static_cache_control how to exclude files from cache - caching

    All Articles

    Geek Answers | 2019