Select the td that contains the tag (Anchor) using jquery

I have HTML code as below. I want to select all td that contains a(anchor) as an immediate child (here First TD) using jQuery.

<table>
  <tr>
    <td><a href="abc.aspx">ABC</a></td>
    <td>Second Level<span> >> </span>
        <div>
          <table>
             <tr>
               <td><a href="efg.aspx">EFG</a></td>
             </tr>
          </table>
        </div>
    </td>
 </tr>

+3
source share
7 answers

Description

Many ways to do it

You can use the css parent selector or jQuery function parent(). Check out the sample and jsFiddle Demo

Example

  • ​$("td > a").parent()
  • $("td > a:parent")

Additional Information

+5
source

This should do the trick:

$("td > a").parent();
+4
source

, jQuery - :has().

$('td:has(> a)')

<td> <a>.

+3

Select the binding using the css selector selector to check that the anchor is a child of td, then call the parent

$('td > a').parent();
+2
source

sort of

$('td').filter(function(){
    if($(this).children('a').length > 0){
        return this;
    });
0
source

If you specify #id for your table, you can do something like this:

$(function(){
    myTds = [];
    $('#mytable td>a').each(function(){
        myTds.push($(this).parent())
    })

//do stuff with your tds
});
0
source
var listOfTD = $("table td > a").parent();

This gives you all td having an anchor as an immediate child from the table. My advice is to provide an identifier to the table, than you should write:

var listOfTD = $("#tableid td > a").parent();
0
source

All Articles