Alegro
Alegro

Reputation: 7956

How to hide multiple divs in a single line of code?

function switch01(){
  $("#contact").hide();
  $("#about").hide();
  $("#mail").hide();
};

This works, but I want all the three lines write in a single one.
I tried with + and , - without result.
In fact I have five divs to hide.

Upvotes: 1

Views: 2631

Answers (2)

Dominic Green
Dominic Green

Reputation: 10258

You can comma seperate your answer like this

$("#contact, #about, #mail").hide();   

Example is here

http://jsfiddle.net/c4swG/

I would normally use a class for this however such as hidden

<div id="contact" class="hidden">contact</div>
<div id="about" class="hidden">about</div>
<div id="mail" class="hidden">mail</div>

$(".hidden").hide();  

http://jsfiddle.net/fBucw/

Upvotes: 5

tomdemuyt
tomdemuyt

Reputation: 4592

See this : http://api.jquery.com/multiple-selector/

$("#contact,#about,#mail").hide(); 

should work

Upvotes: 13

Related Questions