Casey Whitcher Casey Whitcher - 7 months ago 9
Javascript Question

Basic Jquery - If Text exists in a TD then Make a DIV Visible

I'm trying to make a Div on the page Appear if a word exists later on the page? , what i'd actually like it to do is display if any words in a list of 10 appear on the page? The only place I want to look for them is inside of that TD class "sku nobr" after that span class "td-title"

I'm a total rookie at this, I got it to work for a H1 value with this code but I don't know how to do it with the 4523 where it is now??

THANKS!!!!!



<script type="text/javascript">
$(document).ready(function(){
if ($(".sku nobr:contains('4523')").length) {
$("#thingtohide").removeAttr("style").none();
}
});
</script>

<div id="thingtohide" style="display: none;">COOL TEXT TO DISPLAY</div>

<tr class="cart-item-row">
<td class="sku nobr">
<span class="td-title">SKU:</span>
4523
</td>
</tr>




Answer

First, your HTML is not valid. You're missing the <table></table> tags.

<div id="thingtohide" style="display: none;">COOL TEXT TO DISPLAY</div>
<table>
    <tbody>
        <tr class="cart-item-row">
            <td class="sku nobr">
                <span class="td-title">SKU:</span>4523
            </td>
        </tr>
    </tbody>
</table>

Second, your selector is wrong. Replace .sku nobr:contains('4523') with .sku:contains('4523')

$(document).ready(function(){
    if ($(".sku:contains('4523')").length) {
        $("#thingtohide").show();
    }
});