Solved

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

Posted on 2011-03-18
8
132 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
ID: 35165392
Check if this one help : http://api.jquery.com/parents/
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 35165736
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
ID: 35166714
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:gurvinder372
ID: 35166776
what does this return?

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

Accepted Solution

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

Open in new window

0
 

Author Comment

by:uxwebexpert
ID: 35168282
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
need help with share buttons 11 71
JS library vs Js framework 11 23
arrays and radio buttons 10 40
How do i use the await event in php echo 5 19
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

733 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