1

if condition working but else condition not working.

 (function($){
   $(".single-product a").on("click", function(e){
     var link = $(".single-product a").attr("href");    
     if(link == "#"){
        e.preventDefault();
      }else{
        return true;
      }
   });

}(jQuery));
GG.
  • 21,083
  • 14
  • 84
  • 130
Rubel Hossain
  • 2,503
  • 2
  • 22
  • 23

2 Answers2

1

have you tried this? using prop on the attr it might be your jquery library is higher

$(document).on("click",".single-product a", function(e){
     var link = $.trim($(this).prop("href"));  
     (link == "#") ?  e.preventDefault() : '';
 });
Mary Daisy Sanchez
  • 947
  • 1
  • 12
  • 26
1

With the attribute contains selector [name*=”value”]:

$('.single-product a[href*="#"]').on('click', function (e) {
  e.preventDefault();
});
<div class="single-product">
  <a href="https://www.google.com#foo">With #</a>
</div>

<div class="single-product">
  <a href="https://www.google.com">Without #</a>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
GG.
  • 21,083
  • 14
  • 84
  • 130