Will Tuttle
Will Tuttle

Reputation: 450

How to access a <ul> list element in jQuery?

there. I have a div with a list of links in it and I'm trying to store one of the links in a jquery variable, but I'm not sure how to reach it. Unfortunately I can edit the divs or lists directly to add ids (its a yahoo store site), so i need to traverse the object tree.

Here's my practice code

   <div id="newDiv">
        <div id="newerDiv">
            <div id= "squareDiv" style="width: 100px; border:2px solid #a1a1a1;" >Square
            <ul>
                <li><a title="Inca" href="mainazag.html">Maya Inca Aztec</a> </li>
                <li><a title="Maya" href="mainazag.html">Maya Inca Aztec</a> </li>
            </ul>
            </div>
        </div>
    </div>

and this is what I was trying to do access the link with title Maya:

var listitem = $("#squareDiv").find("a[title='Maya']");

any ideas?

thanks

Upvotes: 0

Views: 98

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

the ul is not a descendant of #squareDiv, it is the next sibling. Also the anchor element does not have a class called a so the class selector will not work, you need to use element selector

var listitem = $("#squareDiv").next().find("a[title='Maya']");

Demo: Fiddle

or use the parent div id newerDiv whose child is the ul element

var listitem = $("#newerDiv").find("a[title='Maya']");

Demo: Fiddle

Upvotes: 1

Related Questions