8

I've googled around and found many scripts for hiding and showing DIV contents, as a toggle on button click.

But they are work using ID's.

I would like to do the same thing BUT I want to use a class instead of an id so that if I want to have 20 DIV's that toggle ... Hide / Show I don't have to add extra code.

Here is some code:

<script language="javascript"> 
function toggle() {
    var ele = document.getElementById("toggleText");
    var text = document.getElementById("displayText");
    if(ele.style.display == "block") {
            ele.style.display = "none";
        text.innerHTML = "show";
    }
    else {
        ele.style.display = "block";
        text.innerHTML = "hide";
    }
} 
</script>

<a id="displayText" href="javascript:toggle();">show</a> <== click Here
<div id="toggleText" style="display: none"><h1>peek-a-boo</h1></div>

Can anyone help with this please?

Thanks

Satch3000
  • 47,356
  • 86
  • 216
  • 346

8 Answers8

15

Is jquery an option? Hopefully so, because this does what you want:

http://api.jquery.com/toggle/

$(".class").toggle();
or
$(".class").show();  $(".class").hide();
dpb
  • 1,205
  • 2
  • 9
  • 20
4

Most of the jQuery answers should be pretty easy, but seeing as your example is in regular JS, here's how to do it in JS.

Potential downside: browsers that don't support getElementsByTagName. I tested IE7 and it works, but I'm not sure about lower.

var divs = document.getElementsByTagName('div');

var toggle = function() {    
    for (var i = 0, l = divs.length; i < l; i++) {
        if (divs[i].getAttribute('class') == 'problem') 
            if (divs[i].style.display == 'none') divs[i].style.display = '';
            else divs[i].style.display = 'none';
    }
}

document.getElementById('Toggle').onclick = toggle;

Try it out: http://jsfiddle.net/robert/PkHYf/

Robert
  • 21,110
  • 9
  • 55
  • 65
4

As others have said several times, this is easy in jQuery using a jquery selector and the .hide method. However, since you are asking in a general sense and it is a good idea to know how to do it without a framework, that isn't a complete answer.

Here are your options:

  1. JQuery Method. Just use jQuery selectors and the .hide() method.

    $(".CLASSNAME").hide()

  2. Vanilla JS: Dynamic CSS. One approach is to dynamically append stylesheets to the document head -- you can Alter CSS class attributes with javascript?

  3. Vanilla JS: Modify CSS directly:

        var ss = document.styleSheets;
        for (var i=0; i<ss.length; i++) {
            var rules = ss[i].cssRules || ss[i].rules;
    
            for (var j=0; j<rules.length; j++) {
                if (rules[j].selectorText === ".classname") {
                    rules[j].style.visibility = "none";
                }
            }
        }
    
Community
  • 1
  • 1
slifty
  • 13,062
  • 13
  • 71
  • 109
2

Wouldn't this just be

$('.classname').hide();

Or group all the elements you want to hide within a container div, and then hide that div.

slandau
  • 23,528
  • 42
  • 122
  • 184
2

Using jQuery:

$(".classname").hide();

where classname is the name of the class.

Matthias
  • 12,053
  • 4
  • 49
  • 91
1

Using jQuery Selectors, you can find an ID by:

$("#id")

Changing that to select classes is trivial:

$(".className")

Without using jQuery its a little more non-trivial, but you can check this SO question for some help:

How to getElementByClass instead of GetElementById with Javascript?

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
1

You could simply use $(".className").hide();

The $(".somthing") do the same as $("#somthing"), except it's for a class instead of an ID.

Mash
  • 1,339
  • 7
  • 8
0

This is a great solution to this for Vanilla JS:

https://attacomsian.com/blog/javascript-hide-show-elements-using-css-class

Add/remove a .hidden css class.

Mooktakim Ahmed
  • 1,021
  • 1
  • 10
  • 17