MariaL MariaL - 2 months ago 13
HTML Question

hide div if specific text is displayed in span

I'm trying to hide a a div if a specific text is displayed in a span. I'm using jquery and this is the code:


<div class=“deliveryUpsellText”>

<li class=“error-msg”>
<span> Coupon Code “blabla” is not valid

<button type="button" id="cartCoupon" title="Apply Coupon" class="button applycouponhide" onclick="discountForm.submit(false) ; " value="Apply Coupon"><span style="background:none;"><span style="background:none;">Apply</span></span></button>


if($j(".error-msg span:contains('blabla')")){


It hides the div on click, but it ignores the if statement. So even if the text in the span was 'cat' it still hides the div. Can anybody spot what I've done wrong?
Also as the
button has the onclick event
the deliveryUpsellText is being hidden on click but it's not bound to the form submit so it shows again once the form has been submitted. Anybody know how I can fix that?


jQuery collection is always truthy (because it's an object). You need to check if it contains nodes (selected something). Use length property for this:

if ($j(".error-msg span:contains('blabla')").length) {
        "display": "none"