4

I have the following scenario.

I have a index.php page with the following JQuery code included

jQuery(document).ready(function(){
    jQuery('#sIMG img').click(function() {
        var currentSRC = jQuery(this).attr('src');
        var altSRC = jQuery(this).attr('title');
        var imgID = jQuery(this).attr('id');
        var cat = jQuery(this).attr('name');


        /*Fade, Callback, swap the alt and src, fade in */
        jQuery('#main').fadeOut('fast',function() {
            jQuery('#main').load("detail.php?id="+imgID+"&category="+cat);
            jQuery('#main').fadeIn('fast');

         });
    });
});

Now I have two div tags called #main and #right in the index.php page. When I click on a menu item right changes to a bunch of images, if I click on one of those images the above code should take effect and load into the main div, but it's just not working. the images are located within a div called sIMG. Any help will be appreciated

4
  • Are the images that are loaded in on the right (after clicking a parent image on the right) inserted via javascript or are they just hidden with css when the page loads? Commented Mar 17, 2010 at 12:47
  • Do you use a JS debugger? Check Firebug for any error messages. Commented Mar 17, 2010 at 12:48
  • Kindly elaborate on "just not working". Does it run at all? What happens when you stick alert all over the place? Commented Mar 17, 2010 at 12:48
  • 1
    If the images are dynamically loaded, you'll have to change it to: jQuery('#sIMG img').live('click', function(){ Commented Mar 17, 2010 at 12:51

3 Answers 3

12

Try using live

jQuery('#sIMG img').live("click",function(){
});

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

jQuery('#sIMG img').on("click",function(){
});
Sign up to request clarification or add additional context in comments.

2 Comments

As off jQuery 1.7 you should use .on() instead of live().
In 2017 using 3.0 live is deprecated, jquery.com/upgrade-guide/3.0/…
1

I think what you're doing is setting "click" on the array that is return there. Try this:

jQuery('#sIMG img').each(function() {
    jQuery(this).click(function() {

    });
});

2 Comments

Is there any need to bind event handler like this to a jquery object?
Why would I have to use .live in order to get this work ? Is it because i may have duplicated ids as someone mentioned in this page? I changed the ids to ensure it was unique but I still had the same problem. Using .live worked, but I want to know why this happened?
0

One of the reasons that the jquery click don't work is that you have dupplicates id's in the form.

Comments

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.