19

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,943
  • 21
  • 72
  • 81

3 Answers3

35

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,878
  • 3
  • 14
  • 32
Kasyx
  • 3,170
  • 21
  • 32
26

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
  • 146,340
  • 25
  • 209
  • 204
-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.