18

I have a div with many many html elements like other divs, trs, tds, tables etc

Is it possible to get all the elements which have an id? I know asking $("#test") will give me the specific element with this id

but is it possible to get find("#") or something like this?!

Taryn
  • 242,637
  • 56
  • 362
  • 405
stephan
  • 2,321
  • 4
  • 19
  • 15

3 Answers3

54

$('[id]') returns all elements that have id set

RaYell
  • 69,610
  • 20
  • 126
  • 152
  • Warning - this will be quite slow on a large dom – redsquare Jul 22 '09 at 08:48
  • This is neat. Only issue doing it this way is that you have to read the node id's to know what you're looking at. Not all id's on the page are going to be useful. But if you loop through, reading the node type, you should be able to do things like, say, find the input elements, for example. – Yitzhak Dec 31 '15 at 17:43
  • @yitzhak $('[id]').find(':input') FYI, to find the input elements, there is no need to check the types manually. – E10 Jul 28 '16 at 18:20
4

You can use the following syntax to limit the results:

$('input[id*=test_id]').live('click', callbackFunc());

or

$('.elements_set[id*=test_id]').live('click', callbackFunc());

or in same manner

$('input[name*=test_id]').live('click', callbackFunc());

These are called Attribute Selectors

Reference links:

Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58
0

You should look into documentation on their selectors. This will show you exactly what to do in any situation when you are selecting something.

Also note that you can use more than one selector at a time, like their example:

$("div,span,p.myClass").css("border","3px solid red");
Sneakyness
  • 5,305
  • 4
  • 33
  • 39
  • 5
    Stackoverflow isn't made to "read the manual" answer, it is made to give help, that's why you get my -1. – Clement Herreman Jul 22 '09 at 08:51
  • 3
    This is NOT a read the manual answer. It gives you a list of selectors and everything that you can do with them. It is impossible for me to give him an exact answer without seeing the entire page, knowing exactly what he wants selected. It is extremely straightforward, and is something he will end up looking at quite often until he gets used to selecting things. Would you rather come here and ask a question for each type of new selection you make, or have a list of everything you can do with them as well as correct syntax? – Sneakyness Jul 22 '09 at 09:02
  • Not ONLY that, but it also goes on to list many other useful time-savers, like filters, and ancestry. This is honestly one of the most useful page for people just starting to write in jQuery, and I wish I would have had it when I started learning jQuery. – Sneakyness Jul 22 '09 at 09:04