Hide div if specific text is displayed in between

I am trying to hide a div if a particular text is displayed in between. I am using jquery and this is the code:

HTML

<div class="deliveryUpsellText">
<p>blabla</p>
</div>


<ul>
  <li class="error-msg">
   <ul>
    <li>
     <span> Coupon Code "blabla" is not valid
     </span>
    </li>
   </ul>
  </li>
</ul>


<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>

JQuery

$j('#cartCoupon').click(function(){
if($j(".error-msg span:contains('blabla')")){
    $j('.deliveryUpsellText').css({"display":"none"});
}

});

It hides the div on a click, but it ignores the if statement. Therefore, even if the text in the span is "cat", it still hides the div. Can anyone understand what I did wrong? Just like a button #cartCouponhas an onclick event, the UpsellText dicountForm.submit(false);delivery is hidden when clicked, but does not bind to the submit form, so it displays again after the form is submitted. Does anyone know how I can fix this?

+4
source share
3 answers

jQuery ( ). , (- ). length:

if ($j(".error-msg span:contains('blabla')").length) {
    $j('.deliveryUpsellText').css({
        "display": "none"
    });
}
+1

,

$(document).ready(function () {
    $("#cartCoupon").click(function () {
        var errMsg = $(".error-msg ul li span").text();
        if (errMsg.search("blabla") >= 0) {
            $(".deliveryUpsellText").hide();
        }
    });

});
0

I think you were looking for this query. this is work for me

$(function(){
	
	$('#cartCoupon').on('click', function(){
		$('.error-msg').find('span').each(function(){
			var text = $(this).text();
			if(text.indexOf('blabla') >=0 ){
				$(this).hide();
			}
		});
	})
});
<ul>
  <li class="error-msg">
   <ul>
    <li>
     <span> Coupon Code "blabla" is not valid </span><br>
     <span> Coupon Code is not valid </span><br>
     <span> Coupon Code is not valid </span>
    </li>
   </ul>
  </li>
</ul>
<button type="button" id="cartCoupon" title="Apply Coupon" class="button applycouponhide"  value="Apply Coupon"><span style="background:none;"><span style="background:none;">Apply</span></span></button>     
Run codeHide result
0
source

Source: https://habr.com/ru/post/1652027/


All Articles