Jenny Jenny -4 years ago 126
Javascript Question

Hide parent element if img src = null

I would like to hide the td containing the image if the image src inside of the td is null.

<table>
<tbody>
<tr>
<td>
<p class="centered">
<label class="wrapable">
<img src="null">
<input name="test" value="1" type="radio">
</label>
</p>
</td>

<td>
<p class="centered">
<label class="wrapable">
<img src="null">
<input name="test" value="1" type="radio">
</label>
</p>
</td>

<td>
<p class="centered">
<label class="wrapable">
<img src="null">
<input name="test" value="1" type="radio">
</label>
</p>
</td>

<td>
<p class="centered">
<label class="wrapable">
<img src="...test.gif">
<input name="test" value="1" type="radio">
</label>
</p>
</td>
</tr>
<tbody>




I tried the following, but it would hide more than just the td i was trying to target:

$('td').each(function() {
if($(this).find('img').attr('src') == "null"){
$(this).css('display', 'none');
}
});


Any help would be greatly appreciated, still learning :)

Answer Source

Should be a short line like this: $('img[src="null"]').parent('td').hide();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download