Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts

    find the third td using the parents("tr")

    How do I find the third "td" using the. parents("tr")


    Code:
    $(this).parents("tr").children().find("td:eq(3)")
    thanks

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Try to remove the children() part. With this you will try to find <td> elements beneath the child elements of the <tr> elements. But you want to find <td> elements beneath the <tr> elements directly.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts
    Hi its not working .. thanks for the reply

    here is a more detailed example below


    Code:
    <table>
    
    <tr>
    <td>1</td>
    <td>getthis</td>
    <td onclick="go();">4</td>
    <td>5</td>
    </tr>
    
    <tr>
    <td>1</td>
    <td>getthis</td>
    <td>3</td>
    <td onclick="go();">4</td>
    </tr>
    
    <tr>
    <td>1</td>
    <td>getthis</td>
    <td>3</td>
    <td onclick="go();">4</td>
    </tr>
    
    <tr>
    <td>1</td>
    <td>getthis</td>
    <td>3</td>
    <td onclick="go();">4</td>
    </tr>
    </table>

    Code:
    function go(){
    
    $(this).parents("tr").find("td:eq(2)").html() // get the text "getthis" 
    
    }

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I suspect you will keep having trouble as long as you insist on mixing inline js with jQuery. to attach and onclick to an element in jquery, give the element an id or class and attach the event in the script:

    Code:
    <body>
    
    <table>
    
    <tr>
    <td>1</td>
    <td>getthis</td>
    <td class="go">4</td>
    <td>5</td>
    </tr>
    
    <tr>
    <td>1</td>
    <td>getthis</td>
    <td>3</td>
    <td class="go">4</td>
    </tr>
    
    <tr>
    <td>1</td>
    <td>getthis</td>
    <td>3</td>
    <td class="go">4</td>
    </tr>
    
    <tr>
    <td>1</td>
    <td>getthis</td>
    <td>3</td>
    <td class="go">4</td>
    </tr>
    </table>
    <script type="text/javascript">
    $(".go").click (function(){
    alert($(this).parents("tr").find("td:eq(1)").html()) // get the text "getthis" 
    });
    </script>	
    </body>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •