11

What i want to do is to change the class of the div tag with a toggle button using javascript. So that when i press the button once the div tag gets the show class, and when i press the same button again it gets the hide class. Is this possible?

html:

<a id="togglebutton">Show/hide</a>

<div class="show"> 
   I want this div to toggle between the 'show' class and the 'hide' class
</div>

css:

.show {
    display: block;
}


.hide {
    display: none;
}

js:

function toggle(){
    var hide = document.querySelector(".hide");
    var show = document.querySelector(".show");

}

var hidebutton = document.querySelector(".togglebutton");
hidebutton.onclick = toggle

Would document.getElementById("MyId").className = "MyClass"; be usable somehow?

1
  • #togglebutton not .togglebutton which is a class not an ID Commented Jan 8, 2014 at 18:10

4 Answers 4

11

You can use

if(document.getElementById("MyId").className == "hide")
   document.getElementById("MyId").className = "show";
else
   document.getElementById("MyId").className = "hide";

But you need first to give an ID to the div.. Please find below a working example: http://jsfiddle.net/HvmmY/

Sign up to request clarification or add additional context in comments.

3 Comments

This is old, but saved me. Thank you for the simple and beautiful answer! Also, the example was amazing!
You don't need to remove the existing class first?
@Jarad it's used under the assumption that only one class needs to be present at time. Writing to className overwrites any other class that was present there, thus becoming a 'toggle'
0

If you are just trying to show / hide an element, you can use the style.display property :

hidebutton.onclick = function(){
    var div = document.getElementsByClassName('show').item(0);
    if(div.style.display == '') {
        div.style.display = 'none';
    } else {
        div.style.display = 'block';
    }
}

Hope it helps

Comments

0

I would suggest to toggle the hide class

window.addEventListener("load", function() {
  document.getElementById("togglebutton").addEventListener("click", function(e) {
    e.preventDefault(); // stop the link
    var show = this.innerHTML == "Show";
    document.getElementById(this.dataset.id).classList.toggle("hide",!show)
    this.innerHTML = show ? "Hide" : "Show";
  })
})
.hide { display:none;}
<a href="#" id="togglebutton" data-id="toggleDiv">Hide</a>

<div id="toggleDiv" class="show">
  I want this div to toggle between the 'show' class and the 'hide' class
</div>

2 Comments

This is a good solution (I voted it up). Could be even better if it got the id of the element as it was clicked, because there may be multiple divs to toggle.
@jxwd 7 year old solution. I updated it to 2021
-2

I like to use jquery for adding/removing <div> classes. However, it might not work in your situation.

You might be able to use jQuery on it with toggle:

$('#box').click(function() {
$('#box2').toggleClass('show hide');
});

Working example: http://jsfiddle.net/mFA5F/1/

Information on toggle() can be seen here: http://api.jquery.com/toggle/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.