0

client side takes in some images from server, then base on that create thumbnails pictures. When user click on these thumbnails it does something. However the number of images sent from server can be any number. So now I am stuck, I don't know how to generate click function without writing each one out like below. Can someone guide me to the right direction without giving me the actual answer?

            $("#thumb-0").click(function(){
                index=0;
                switchHouse(index);
            });
            $("#thumb-1").click(function(){
                index=1;
                switchHouse(index);
            });
            $("#thumb-2").click(function(){
                index=2;
                switchHouse(index);
            });
                            ...
            $("#thumb-X").click(function(){
                index=arrayLength;
                switchHouse(index);
            });

I tried the following, but doesnt work obviously:

            for (var i=0; i<topHouse.length; i++){
                $("#thumb"+i).click(function(){
                    index=i;
                    switchHouse(index);
                });
            }
2
  • 2
    same with @Asad's solution, but a demo here: jsfiddle.net/aGfNS/1 Commented Sep 26, 2013 at 18:04
  • 1
    You are missing the dash in "#thumb"+i it should be "#thumb-"+i Commented Sep 26, 2013 at 18:06

4 Answers 4

4

When iterating, each function closes over the variable i. When the function is evaluated the value of i has reached the end of the iteration. In other words, when you click a thumbnail, the value of i is topHouse.length, and so the function essentially sets index = topHouse.length.

Try using a closure, so that each handler has its own index value:

for (var i=0; i<topHouse.length; i++){
    $("#thumb"+i).click((function(index) {
        return function() {
            switchHouse(index);
        }
    })(i));
}
Sign up to request clarification or add additional context in comments.

3 Comments

can u tell me why we need return function(){}, I am pretty new to this
@user308553 Sure. You see, we want to be able to refer to the value of i at that particular iteration, without having our handler close over i and keep up to date on its value. When I say "close over", I mean that if you refer to i in a function directly, the function will latch onto the actual i variable, which keeps changing with every iteration. To avoid this, we pass the value of i to an anonymous function (inside click) that creates your desired handler function and returns it. The inner function is latching on to the index parameter, which will have a value of i.
@user308553 You should read up on closures in Javascript. That will make this clearer.
3

You can use the Starts With

$( document ).ready(function() {

$("[id^='thumb']").click(function() {
    switchHouse( $(this).index() );  // or add $(this).index()+1

});

Comments

2

Here's one solution.

Change your markup to this:

<whatever class="thumbnail" data-index="1" />

And your handler to this:

$('.thumbnail').click(function() {
    switchHouse($(this).data('index'));
});

Comments

1

Add a class to each image with a unique thumb ID so you can target it with Jquery. Then do this.

<img id="thumb-25" class="aThumb" src="...">

 $('.aThumb').click(function(e){
var getTheID = $(this).attr('id');
getTheID = getTheID.substring(6);

// switchHouse(getTheID);
  alert(getTheID);
});

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.