Hirvesh
Hirvesh

Reputation: 7982

Finding all divs with a link which has a specific id in jquery

I'm looking for a way to select the all the divs which has a sub link element with a specific id.

Structure

<div>
<a href="#" id="123">link</a>
</div>

How do I select the div with jquery?

Upvotes: 0

Views: 113

Answers (2)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195992

You can use the :hasdocs selector.

$('div:has(>#123)')

as mentioned, you should not use numbers for ids. You can prefix them with something like id_ and remove that when you want the actual number.


update

added the > to the selector used by the :has to only select the div that has the link as a direct child.

Upvotes: 4

user241244
user241244

Reputation:

$("#link-123").closest("div")

will get you the parent. I'd suggest giving the div a class so that, in case you have to insert an intervening div later, the code works reliably. So it'd be something like:

$("#link-123").closest(".link-container")

Upvotes: 2

Related Questions