JQuery: click function does not capture click event on hyperlink

I have the following code snippet

<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> $('body').on('click', 'a.wishlist_item', function(){ alert('asas'); return false; }) </script> </head> <body> <a class="wishlist_item" id="wishlist_item" href="#" >Add to wishlist</a> </body> </html> 

The code should be warned when I click on the hyperlink with the wishlist_item class. but its not working .. is there something i can do wrong in this code?

+4
source share
1 answer

You need to bind the event after the existence of the element. Use the ready event to fire code when the whole page loads:

 $(document).ready(function(){ $('body').on('click', 'a.wishlist_item', function(e){ alert('asas'); e.preventDefault(); }); }); 
+7
source

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


All Articles