18

by topic I have some divs with id = "loader".

In my jQuery code I have

  $("#loader").hide(),

but it works only with the first div.

How could I hide all the divs?

Thanks a lot.

Rachel Gallen
  • 27,043
  • 20
  • 72
  • 79

3 Answers3

34

Having more than one element with the same ID is not valid HTML. You can only have one element with the ID (#loader) in the whole page. That's why jQuery is hiding only the first element. Use the class instead of the id:

$('.loader').hide();
Guillaume Georges
  • 3,718
  • 3
  • 12
  • 30
Kasyx
  • 3,100
  • 20
  • 30
25

The ids of html elements should be unique so you better use class with all element and use class selector to hide them all.

$('.className').hide();

If it is not possible for you to assign common class to them for instance you can not change the source code you can use Attribute Equals Selector [name=”value”].

 $("[id=loader]").hide();
Adil
  • 143,427
  • 25
  • 201
  • 198
-3

A way to hide all items of the same ID was as follows

$( "#hide" ).click(function() {
  $('div#hidden').hide();
});
<div id="hidden">ID Number 1</div>
<div id="2">ID Number 2</div>
<div id="hidden">ID Number 1</div>
<div id="2">ID Number 2</div>
<div id="hidden">ID Number 1</div>
<a href="#" id="hide">Hide Div</a>

Hope you can find this helpful.