Deckard
Deckard

Reputation: 1433

Why is my function not called on 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 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

Answers (4)

Vina
Vina

Reputation: 759

or

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

Upvotes: 0

BritishDeveloper
BritishDeveloper

Reputation: 13349

should be

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

Upvotes: 2

Delan Azabani
Delan Azabani

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

rfunduk
rfunduk

Reputation: 30432

Because the DOM hasn't been loaded yet:

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

Upvotes: 10

Related Questions