2

Here is my HTML code.

<p>Men's Shirt (on hanger) 
<span class="cart_minus"> - </span>
<span class="cart_add"> + </span> 
<label class="text_right"> 2 Suits </label> </p>
<div class="total"> Total <span>£22.00</span></div> 
<a href="" class="order_now"> Order now </a>

JQuery code.

$(document).ready(function() {
$('.cart_minus').click(function () {
            alert("hai");
});
});

When I put same code in console then alert is shown. How could I solve this.

Sandra
  • 418
  • 1
  • 5
  • 14

2 Answers2

4

You might be missing the JQuery library in your HTML file. Your code works well. Also if they are created dynamically you can use this code below which will add an event listener to the dynamically created element and attach it to the document object.

$(document).on('click','.cart_minus', function () {
    alert("hai");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Men's Shirt (on hanger) 
<span class="cart_minus"> - </span>
<span class="cart_add"> + </span> 
<label class="text_right"> 2 Suits </label> </p>
<div class="total"> Total <span>£22.00</span></div> 
<a href="" class="order_now"> Order now </a>
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
1

All of your code is working fine for me. Just make sure you added jQuery before using it.

<p>Men's Shirt (on hanger) 
<span class="cart_minus"> - </span>
<span class="cart_add"> + </span> 
<label class="text_right"> 2 Suits </label> </p>
<div class="total"> Total <span>£22.00</span></div> 
<a href="" class="order_now"> Order now </a>

<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('.cart_minus').on('click', function () {
      alert("hai");
        });
    });
</script>
kevinniel
  • 1,088
  • 1
  • 6
  • 14