views:

60

answers:

3

Hi,

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

+1  A: 
$("*[id]")

should work, and - for the record -

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

is the opposite.

Tomalak
works great, thanks !
div1n
+1 But I think I'd probably limit the query to descendants of `body` just to make it a little quicker. `$('body [id]')`
patrick dw
@patrick: Marginally, if at all.
Tomalak
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 `<script>` tag is innocuous... but then maybe not.
patrick dw
@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
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.
patrick dw
A: 

Use it:

$("div[id]")

See in jsfiddle.

Topera
A: 
$('[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

Rocket