More efficient way to do parent parent parent etc in jquery

0 votes

In this script I'm writing, I find myself using .parent() up to seven times in a row to get elements. While this works, it seems like there could/should be an easier way to do this/ function I'm unaware of. Any ideas other than more specific classes/ids on more elements?

Basically, my question boils down to accessing the div with id outer when I have a reference to the span with id 'innerSpan' in the html below:

<div id='outer'>
    <a href="some_url">
        <span id="inner">bla bla</span>
    </a>
</div>
Jun 27, 2022 in Web Development by gaurav
• 23,260 points
432 views

1 answer to this question.

0 votes

The jQuery parent() and parents() methods return the elements which are ancestors of the DOM.
HTML.

parent() Method parents() Method
It returns only an element that is the direct parent. It returns all elements that are ancestors to the selected element
answered Jun 27, 2022 by rajatha
• 7,680 points

Related Questions In Web Development

0 votes
0 answers
0 votes
1 answer

How to display current element in jQuery?

<! DOCTYPE html> <html> <head> <script> $(document). ready(function(){ $("button"). click(function(){ $("p"). text("Welcome to ...READ MORE

answered Jun 27, 2022 in Web Development by rajatha
• 7,680 points
1,371 views
0 votes
0 answers

How to add a link in Jquery PrettyPhoto to download the image

I am using Jquery PrettyPhoto to have ...READ MORE

Jun 29, 2022 in Web Development by gaurav
• 23,260 points
904 views
0 votes
1 answer

How to Fade In/Out multiple texts using CSS/jQuery like on Droplr?

The jQuery fadeIn() method is used to ...READ MORE

answered Jun 29, 2022 in Web Development by rajatha
• 7,680 points
1,434 views
0 votes
1 answer

How to use jquery with asp.net ajax?

If you weren't aware, Microsoft is planning ...READ MORE

answered Oct 15, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
752 views
0 votes
1 answer

Is 'sparkline' a method?

I suggest you to check 2 things That jquery.sparkline.js is actually ...READ MORE

answered Nov 9, 2018 in Apache Spark by Frankie
• 9,830 points
1,276 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,520 points
1,322 views
0 votes
1 answer

Error: Global Variable is not accessable to local function

Hey kartik, A variable declared outside a function has a ...READ MORE

answered Feb 19, 2020 in PHP by Niroj
• 82,840 points
1,095 views
0 votes
1 answer

how to use substr() function in jquery?

To get substring of a string in ...READ MORE

answered Jun 27, 2022 in Web Development by rajatha
• 7,680 points
1,267 views
0 votes
1 answer

How to loop through array in jQuery?

Generic loop: var i; for (i = 0; i ...READ MORE

answered Jun 28, 2022 in Web Development by rajatha
• 7,680 points
2,236 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP