$('.pp').click(function() { alert(); });

asdf

...">

Why is my function not triggered by a click?

<script type="text/javascript"> $('.pp').click(function() { alert(); }); </script> <p class=pp>asdf</p> <p class=pp>asdf</p> <p class=pp>asdf</p> 

Why is the function not called on a click event?

This should be a very stupid and stupid question, but I don’t know what I am missing.

+1
source share
4 answers

Since the DOM is not loaded yet :

 $(document).ready( function() { // ...your code... } ); 
+10
source

it should be

 <script type="text/javascript"> $(function(){ $('.pp').click(function(){ alert(); }); }); </script> 
+2
source

This is because you are attaching a click event to a node that is not there yet. Place the code after the HTML nodes or call it on load or DOMContentLoaded .

0
source

or

 <script type="text/javascript"> $(document).ready(function(){ $('.pp').click(function(event){ alert(); }); }); </script> 
0
source

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


All Articles