12

I want to implement .draggable class to all elements in my document, with an existing id using jQuery 1.4.2

<div id="blabla">asdsda</div> -> OK
<div>dsds</div> -> NOT OK

Is this possible ?

i tried : $("*[id!=null]") but i does not work :s

Andy E
  • 326,646
  • 82
  • 467
  • 441
div1n
  • 123
  • 1
  • 4
  • Possible duplicate of [jQuery selectors on custom data attribute that are not empty](http://stackoverflow.com/questions/22177815/jquery-selectors-on-custom-data-attribute-that-are-not-empty) – Caio Wilson Apr 29 '16 at 18:43
  • 1
    To select with js selecter, do `document.querySelectorAll('[id]')` – Shivam Jha Nov 12 '21 at 08:24

3 Answers3

20
$("*[id]")

should work, and - for the record -

$("*:not([id])")

is the opposite.

Tomalak
  • 322,446
  • 66
  • 504
  • 612
  • +1 But I think I'd probably limit the query to descendants of `body` just to make it a little quicker. `$('body [id]')` – user113716 Sep 10 '10 at 13:52
  • Marginally, yes. If at all? Well, if there's one additional tag, (HTML for example) then of course there must be *some* difference, though marginal. Also, what if a tag in the HEAD has an ID? Maybe calling `.draggable()` on a ` – user113716 Sep 10 '10 at 15:20
  • @patrick: From a "time to execute" perspective, I do not expect any difference that's more than academic. From a "be specific to avoid bugs" perspective, your's may be the better choice, however. – Tomalak Sep 10 '10 at 15:30
  • 1
    Fair enough. :o) I certainly don't disagree with your assessment of the performance difference. Such is the case of many performance-related 'best practices' we may do. – user113716 Sep 10 '10 at 15:40
5
$('[id]')

This will grab all elements that have an 'id' attribute.

Or to get all divs with an 'id' attribute you can do:

$('div[id]')

Has Attribute Selector

gen_Eric
  • 214,658
  • 40
  • 293
  • 332
0

Use it:

$("div[id]")

See in jsfiddle.

Topera
  • 11,829
  • 14
  • 65
  • 101