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
  • 338,112
  • 86
  • 474
  • 445
div1n
  • 123
  • 1
  • 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
  • 332,285
  • 67
  • 532
  • 628
  • +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
  • 223,194
  • 41
  • 299
  • 337
0

Use it:

$("div[id]")

See in jsfiddle.

Topera
  • 12,223
  • 15
  • 67
  • 104