Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

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

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
uxwebexpert
Asked:
uxwebexpert
  • 2
  • 2
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
Check if this one help : http://api.jquery.com/parents/
0
 
Gurvinder Pal SinghCommented:
please share the html structure for the same

or little reduced version of it

$(this).parent().parent().sibling('a:first').append(text);
0
 
uxwebexpertAuthor Commented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Gurvinder Pal SinghCommented:
what does this return?

$(this).parents("li:last").find(a span).html();
0
 
leakim971PluritechnicianCommented:
$(this).parents("ul:eq(2)").find('a:first')

Open in new window

0
 
uxwebexpertAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now