Search code examples
jqueryif-statementcontainsclosest

Jquery - conditional contain of the closest elem


I am trying to refresh a page when I click on the "delete elem", only if the closest "product title" contains a specific text. The jquery code below does not work.. If you have any idea, thank you.

$( document ).ready(function(){
  $(".delete-item").on( "click", function( reloadEvent ) {
    if( $( reloadEvent.target ).closest(".Product-details").find(".Product-title:contains('XYZ')").lenght)  
      {location.reload.bind(location);}
    else {}
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="Product-details">
  <div class="Product-header">
    <span class="Product-title">Product title A</span>
  <div/>
  <div class="Product-footer">
    <div class="delete-action">
      <a href="#" class="delete-item">DELETE</a>`
    </div>
  </div>
</div>
<div class="Product-details">
  <div class="Product-header">
    <span class="Product-title">Product title B</span>
  <div/>
  <div class="Product-footer">
    <div class="delete-action">
      <a href="#" class="delete-item">DELETE</a>`
    </div>
  </div>
</div>


Solution

  • i'll write that:

      $(".delete-item").on( "click", function(  ) {
        if( $( this ).closest(".Product-details").find(".Product-title:contains('XYZ')").length)  
          {location.reload.bind(location);}
        else {}
       })