Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8563993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:05:48+00:00 2026-06-11T17:05:48+00:00

My code looks like this, in closeup: <h2> <span class=stuff>[<a id=someid>stuff</a>]</span> <span class=moreStuff>Another test</span>

  • 0

My code looks like this, in closeup:

<h2>
<span class="stuff">[<a id="someid">stuff</a>]</span> <span class="moreStuff">Another test</span>
</h2>

I’ve found a way to select my a element, and attach an id to it. What I need to do now is select its parent <h2> element, but not the <span> element. How can I do that (JQuery allowed)?

Edit: when I retrieve the selected <a>s, I get an array of them (there’s lots of these structures on my page). When I try to write myArray[someIndex].closest("h2"), it says that the element does not have a closest() method. How would I go about this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:05:49+00:00Added an answer on June 11, 2026 at 5:05 pm

    One ways is to use the .parents() method of jQuery, with a selector. Something like this.

    $("#someid").parents("h2");
    

    Update:

    You can use the .closest() method with a selector, to only get the closest parent that match the selector.

    $("#someid").closest("h2");
    

    Update 2:

    It would be a bit more work to do it with plain JavaScript. Not sure if it is the most efficient, but one way would be to select the element with document.getElementById() and then get a reference to its parent through the parentNode property. Then you would have to check if it is an h2 element, and if not, look at that elements parent node, and so on.

    You could check the jQuery source and see how they have implemented the closest method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my HTML code looks like this <TR Row=1 CLASS=ThemeAlignCenter> <TD id=wrdActive_Row1 style=width: 50px CLASS=rdThemeDataTableCell><SPAN
example: http://jsfiddle.net/kuTLf/ code looks like this: <div id=main> <div id=slideshow class=pics> <div id=nav></div> <img
Getting Expected ',' or '{' but found '#44559' error. My code looks like this:
My original code looks like this: class a{ ... char buff[10]; } and im
My code looks like this: ... # class Site(Resource) def render_POST(self,request) otherclass.doAssync(request.args) print '1'
I have an abstract class where source code looks like this: /* * @assert
My code looks like this: $result = mysql_query(SELECT * FROM `picdb` WHERE `picid` =
The code looks like this, <a class=foo title=a></a> <a class=foo title=b></a> <a class=foo title=c></a>
Usually code looks like this: (one-thing (another-thing arg1 (f arg5 r)) (another-thing arg1 (f
my HTML code looks like this <TABLE style= border=0 CLASS=rdThemeDataTable id=dtWardData cellspacing=0> <COL id=wrdActive></COL>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.