Furkan Gözükara
Furkan Gözükara

Reputation: 23800

Jquery how to bind click event on dynamically created elements?

I tried the below code but it is not working

<script type="text/javascript">
    $(document).ready(function () {
        $('body').on('click', '.pg_previous,.pg_next', function () {
            jQuery("img.lazy").lazy({});
            alert('ddsda');
        });
    });
</script>

Jquery 1.9.1

Upvotes: 5

Views: 26506

Answers (2)

RicardoE
RicardoE

Reputation: 1725

here try this:

<script type="text/javascript">
$(function(){
    $('body').on('click', '.pg_previous,.pg_next', function () {
        jQuery("img.lazy").lazy({});
        alert('ddsda');
    });
});
</script>

Upvotes: 11

Sushanth --
Sushanth --

Reputation: 55740

You forgot the DOM ready handler

Encase your code inside the ready handler and should work fine unless you have any errors showing up in our console.

$(function() {
    // Your code here
});

Upvotes: 3

Related Questions