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 7779447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:38:37+00:00 2026-06-01T18:38:37+00:00

I have a following code in my html file and i want to access

  • 0

I have a following code in my html file and i want to access the grand parent of an element like the table which is the grand parent of td in the following code. For this I used the following jquery syntax but it displays “undefined”. can any one guide me where the problem is ?

  <script type="text/javascript">

   $(document).ready(function () {
     alert($('td[id="4"]').parents().eq(1).attr("id"));
  });

</script> 

<title></title>
</head>
<body>
<form id="form1" runat="server">
  <div  class="ui-layout-center" id="center" runat="server">
     <table id="Table1" >             
        <tr id="tr1">
            <td id="3" class="cell" >
               first cell 
            </td>
            <td id="4" class="cell">
            second cell
            </td>
        </tr> 
     </table>
    <br />
    <br />
  </div>
</form>
</body>
</html>
  • 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-01T18:38:37+00:00Added an answer on June 1, 2026 at 6:38 pm

    First off, you can’t run your code until the DOM is loaded. That means that you either must place it AFTER the relevant HTML in your source file or use jQuery’s method of waiting until the DOM is loaded:

    $(document).ready(function() {
        alert($('td[id="4"]').parents().eq(0).attr("id"));
    });
    

    Then, your code:

    $('td[id="4"]').parents().eq(0)
    

    Is getting the first parent of the matching td element. That first parent will be the tr tag which will alert it’s ID, not the <table> element’s id as you can see here: http://jsfiddle.net/jfriend00/rxUEp/

    If you want the table element’s ID, then it’s best to specify the actual parent you want with code like this:

    $(document).ready(function() {
        alert($('td[id="4"]').closest("table").attr("id"));
    });
    

    Further fixing things up, ID values should start with an underscore or a letter, not a number and once you have a valid ID, you should use a much more efficient selector like this:

    <table id="Table1" >
        <tr id="tr1">
            <td id="cell3" class="cell" >
               first cell 
            </td>
            <td id="cell4" class="cell">
            second cell
            </td>
        </tr> 
    </table>
    
    $(document).ready(function() {
        alert($('#cell4').closest("table").attr("id"));
    });
    

    P.S. Another reason it’s good to use .closest("table") to find the desired parent is that tables all have an implicit <tbody> tag (even if it’s not in your HTML) which means that the actual <table> tag is actually the 3rd parent up from your <td>. With .closest("table"), you don’t have to worry about these find details as jQuery just finds the one you have specified.

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

Sidebar

Related Questions

I have the following code which loads and html file into a webview -
I have the following html code. <html> <body> <div style=max-width:1600px; min-width:900px; > <table> </table>
I have the following HTML code, what I want to do is to split
I have the following code: if (System.IO.File.Exists(htmlLocation)) { vEarthBrowser.ObjectForScripting = this; vEarthBrowser.Url = new
I have the following code snippet from a HTML file: <div id=rwImages_hidden style=display:none;> <img
I have a html code which I saved in home.txt file and placed it
i have following HTML code:- <table class=results> <tr> <td> <a href=..>link</a><span>2nd Mar 2011</span><br>XYZ Consultancy
I have the following code: HTML <label>File Upload: </label> <input type=file name=file[] /> jQuery
I have the following html code to upload an image: <input type=file name=before size=40>
Just a (hopefully) quick question, I have the following HTML code: <tr> <td><img src=img/icons/file_pdf.png></td>

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.