Orden de ejecución de Javascript/JQuery

I have multiple instances of a div ".gallery" and I want to pick up the id of the div the particular instance of .gallery is sitting in on click. This would then populate a variable that I can use to make the image only change in that div's gallery. Problem is that the page seems to try to do the image swap, then changes the variable, so the image doesn't change on the first click, and-worse-the first time you click on a different gallery it changes the image of the last gallery you interacted with. Currently there is a javascript alert that I have pop up every time you click a div that reports the div you clicked. I have the page in its current state here if you want to play around with it. www.ryanscasey.com/redesign

The straight javascript is as follows:

$(document).ready(function() {

    var clickedDiv;

    $('.gallery').click(function() {
        clickedDiv = $(this).parent().parent().attr("id");
        $('#' + clickedDiv + ' .thumbs img').click(function() {
            $('#' + clickedDiv + ' .largeImage').html($(this).attr('alt'));

preguntado el 22 de mayo de 12 a las 19:05

1 Respuestas

I think this is what you want. It doesn't sound like you need to bind more than one click event. The way you had it, it would bind a click event any time a .gallery item was clicked. If this doesn't work, let me know, and we'll try to work from there.

$(document).ready(function() {
    $('.gallery .thumbs img').click(function() {
        var $this = $(this),
            $gallery = $this.closest('.gallery'),
            $large = $gallery.find('.largeImage');


EDIT: I took a look at your site and I have adjusted my code. You want to bind the click event to the thumbnail not the entire gallery.

contestado el 22 de mayo de 12 a las 19:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.