5

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

5
  • This fiddle works without the lazy call, maybe look there, or move the alert ahead of the lazy call. jsfiddle.net/5SyS3 Commented Jul 9, 2013 at 21:13
  • @JasonP this is cancelling the event :( "return false;" Commented Jul 9, 2013 at 21:23
  • Try replacing return false; with e.preventDefault() Commented Jul 9, 2013 at 22:55
  • +1 for using body as selector instead of class selector (for dynamically created element) Commented Mar 15, 2014 at 17:56
  • Here's a detailed article on how to bind click event for dynamic element codepedia.info/2015/02/click-event-for-dynamic-button-jquery Commented Aug 17, 2015 at 5:15

2 Answers 2

11

here try this:

<script type="text/javascript">
$(function(){
    $('body').on('click', '.pg_previous,.pg_next', function () {
        jQuery("img.lazy").lazy({});
        alert('ddsda');
    });
});
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

This did it for me! Thank you! Attaching the click event to the parent element and selecting its children is the most efficient way.
both are equivalent, but for this particular version of jQuery I often found the long version doesn't work, and the shortcut do.
3

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
});

1 Comment

ok i believe another code cancels default action so it cancels binded action too thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.