Reputation: 1433
<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 the function is not called on click event?
It must be very silly and stupid question, but I don'w know what I'm missing.
Upvotes: 2
Views: 178
Reputation: 759
or
<script type="text/javascript">
$(document).ready(function(){
$('.pp').click(function(event){
alert();
});
});
</script>
Upvotes: 0
Reputation: 13349
should be
<script type="text/javascript">
$(function(){
$('.pp').click(function(){
alert();
});
});
</script>
Upvotes: 2
Reputation: 81384
This is purely because you are attaching a click event to a node that doesn't actually exist yet. Put the code after the HTML nodes, or call that upon the load
or DOMContentLoaded
events.
Upvotes: 0
Reputation: 30432
Because the DOM hasn't been loaded yet:
$(document).ready( function() {
// ...your code...
} );
Upvotes: 10