views:

49

answers:

2
+1  A: 

You can combine the :not() and has-attribute selectors, like this:

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

A few notes though, you currently have 2 elements with the same ID, this is invalid, IDs should be unique. Also you shouldn't use the selector exactly as I have it above, it should be within something, for example $("#test :not([id])") to narrow it down...it's very expansive otherwise.

Nick Craver
I would also note it selects everything *inside* `<p id="test"></p>`, which isn't always welcomed.
Kobi
@Kobi - I don't follow you there...it doesn't select everything, it wouldn't select the first element, only the second and third like he asked for...
Nick Craver
@Nick: ohwoh, I wrote that when the description was not there, I'll delete it
jAndy
If you have `<p id="test"><strong>in the P</strong></p>`, the `<strong>` element gets selected too. It's good for the posted code, of course. Isn't too important, really :) (actually, I suppose I only mentioned it because I had a similar bug once, with bubbling events)
Kobi
+3  A: 

You can't have 2 elements with the id "test" but if the code was as follows:

<div id="test">
<p id="test2"></p>
<p></p>
<p></p>
</div>

then you could use

$("#test p").not("#test2")

or

$("#test p:not(#test2)")

to select just the other two paragraphs.

See http://api.jquery.com/not/ for the documentation for the not() method (first option) or http://api.jquery.com/not-selector/ for the :not() selector (second option).

Note: this won't select "everything" but rather the second and third paragraph elements. I assume that's what you meant :)

njk
Yes that is. Thanks very much
ultimatebuster
You're welcome! Can you 'accept' the answer by clicking on the checkmark, if this met your needs?
njk