Jump to content

if( not working ){ for this noob }


Who8MyFish

Recommended Posts

your if/else is done initially when the page is loaded and index is 0

 

try this instead..

 

$(document).ready(function() {
    var index = 0;
    $("#left-button").click(function() {
        if(index > -5)
            index--;
        else
            index = 0;
        $("#index").html("<span>" + index + "</span>");
    });       
});

to clarify the problem with your original code:

$(document).ready(function() {
    var index = 0;
    if (index > -5) {
        $("#left-button").click(function() {
            index--;
            $("#index").html("<span>" + index + "</span>");
        });
    }
    else if(index == -5){
        $("#left-button").click(function() {
            index = 0;
            $("#index").html("<span>" + index + "</span>");
        });
    }
   
        
});​

 

this was basically saying to do this when the page was loaded..

 

index = 0

if(index > -5)

  initialize the function that listens for a click on left-button and decrements index when clicked

else

  initialize the function that listens for a click on left-button and sets index to 0 when clicked

syntax is not different. you can wrap in curly brackets if that is what you mean, it's just unnecessary if there is only 1 line in the block.

 

your problem wasn't syntax, it was the logic.

 

you didnt have a function that did something differently depending on a value. you had two separate functions with the same object and trigger, which function was basically created and used depended on what the value of index was the first time through, on document ready... as index was set to 0 right before this, it always used the first one.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.