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

Answers


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


Need Your Help

Java Pattern RegEx for Conditional Removal of Whitespaces

java regex

I have searched for an answer for a couple of hours now and still nothing comes close to solving a specific programming predicament. This is neither for school or work. I'm developing an app that n...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.