Chris Dutrow
Chris Dutrow

Reputation: 50432

jQuery - Select tag , then child tag with text, then sibling tag

I have some html that looks like this:

<dl>
    <dt>Label 1</dt>
        <dd>
            Yadda yadda yadda
        </dd>
    <dt>Label 2</dt>

I would like to get at the text that says "Yadda yadda yadda". I figure I need to tell jQuery to do something like "Select tag where the parent is a tag that contains the text "Label 1".

What is a good way to do this?

Thanks!

Upvotes: 0

Views: 605

Answers (2)

Shaz
Shaz

Reputation: 15887

$("dl dd");

Example: http://fiddle.jshell.net/gE5RR/1/

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 817238

It is not the parent tag, it is the previous sibling. You can do:

var text = $('dt:contains("Label 1")').next().text();

Of course this would also select any other dt element that contains the text Label 1.

If the element is always the first dd element, you can also do:

var text = $('dl dd').eq(0).text();

Upvotes: 4

Related Questions