Solved

looking for a better way to traverse up the dom using jquery

Posted on 2011-03-18
8
128 Views
Last Modified: 2013-11-19
Hi,

I am looking for a cleaner, better way to write the following line of code.  Within my current infrastructure I can't add id's or classes to identify specific dom elements.  I need to traverse up the dom a specific # of steps to append to the element.  I would like to find a better way of traversing up instead of chaining.  Thanks much, the line of code is here...

$(this).parent().parent().parent().find('a:first').append(text);
0
Comment
Question by:uxwebexpert
  • 2
  • 2
  • 2
8 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Check if this one help : http://api.jquery.com/parents/
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
please share the html structure for the same

or little reduced version of it

$(this).parent().parent().sibling('a:first').append(text);
0
 

Author Comment

by:uxwebexpert
Comment Utility
With the below example I want to append where li a span = Test, when I click on either 1, 2, 3, 4, or 5.               



                   <ul>  
                                    <li><a><span>Test ></span></a>
                                          <ul>
                                             <li><a href="#"><span style="display:none">Select Category > </span>TestA</a>
                                                      <ul>
                                                            <li><a>1</a></li>
                                                            <li><a>2</a></li>
                                                            <li><a>3</a></li>
                                                            <li><a4</a></li>
                                                            <li><a>5</a></li>
                                                      </ul>
                                             </li>
                                                             </ul>
                                    </li>
                              </ul>
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
what does this return?

$(this).parents("li:last").find(a span).html();
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
$(this).parents("ul:eq(2)").find('a:first')

Open in new window

0
 

Author Comment

by:uxwebexpert
Comment Utility
leakim971 - thanks! it worked.  It was referencing the wrong ul, but I am sure it was how I submitted my markup.  I appreciate your help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now