Jim
Jim

Reputation: 723

Select an element with jQuery selector regardless of ID

I have the following code:

<div>
 <div id="someID"><h1>Blah</h1></div>
 <div id="someID2"><h1>Blah</h1></div>
</div>

and I am trying to select in jQuery (for the accordion in jQuery UI). However, the following does not work unless I remove the ID from the div.

$('> div > h1')

Is there some way to tell the selector to ignore the ID, or am I going about this the wrong way?

Edit: The actual use I'm going for is in the sortable accordion in jQuery UI, with IDs on the div elements. An example of what I'm doing, with source, can be found here:

http://jqueryui.com/demos/accordion/#sortable


Edit: As I have a feeling this may be related to jQueryUI, rather than the selectors themselves, below is the code I'm actually trying to run.

http://jsfiddle.net/Zgkj4/1/

Working example without ID in div:

http://jsfiddle.net/Zgkj4/2/

Upvotes: 1

Views: 1898

Answers (5)

Hussein
Hussein

Reputation: 42808

http://codylindley.com/jqueryselectors/. A list of all jquery selectors. it should help you allot.

Upvotes: 1

hunter
hunter

Reputation: 63512

You could try selecting the <h1> with a parent <div> that has an id attribute

$("div > div[id] > h1")

working example: http://jsfiddle.net/YjSvU/


ignores div w/o id: http://jsfiddle.net/YjSvU/1/

Upvotes: 2

alex
alex

Reputation: 490153

Your selector needs to be a string, so wrap it in single (') or double quotes (").

Also > div does not make sense, as > is direct descendant / child selector, and you have no parent on the left hand side (or otherwise context). Remove the leading >.

See it not working on jsFiddle.

Upvotes: 1

David Houde
David Houde

Reputation: 4778

Use quotes, and im not sure about the first >

This should work:

$("div > h1")

Upvotes: 0

a&#39;r
a&#39;r

Reputation: 36999

Yes, your selector should be a string, like this:

$('div > h1')

Upvotes: 1

Related Questions