Mukesh
Mukesh

Reputation: 7778

How to check if an element exist in a td

I am having following html

<td id="td0" style="width:180px;" class="even"> 
        <strong>Bedroom</strong> <br>
        <span class="manufacturer_links"> 
            <a href="http://testsite.com/bedroom-furniture/beds.htm?manufacturer=2441">Coaster Furniture&nbsp;Beds</a>
        </span>
    <br>    
        <span class="manufacturer_links"> 
        <a href="http://testsite.com/bedroom-furniture/bed-frames-headboards.htm?manufacturer=2441">Coaster Furniture&nbsp;Bed Frames &amp; Headboards</a>
        </span>
    <br>
    <br>            
</td>

I do not want to display whole

<td id="td0" style="width:180px;" class="even"> class and id of td will be updating dynamically.

if <span class="manufacturer_links"> does not exist in the td

How to do that?

Upvotes: 0

Views: 2770

Answers (3)

tozlu
tozlu

Reputation: 4865

You can check if it exists, then do anything you want:

if( $('#td0 .manufacturer_links').length == 0) { //the class we look for does not exist in td0 element
    //do anything, such as delete the td?
    $('#td0').remove();
}

Upvotes: 1

Chris Mukherjee
Chris Mukherjee

Reputation: 841

jQuery:

$("td#td0").not(":has(span.manufacturer_links)").css("display", "none");

Upvotes: 1

VisioN
VisioN

Reputation: 145388

Use selector magic:

$('td:not(:has(span.manufacturer_links))').addClass('collapsed');

Upvotes: 3

Related Questions