Who8MyFish Posted March 27, 2012 Share Posted March 27, 2012 ok so I've checked my syntax and everywhere I checks says I'm right so I thought I'd ask one of you folks. So load this up and click the little black <div>, it should deincrement to a max of -4 and then reset to 0 but it just deincrements without end. http://jsfiddle.net/xyLfP/ Quote Link to comment https://forums.phpfreaks.com/topic/259823-if-not-working-for-this-noob/ Share on other sites More sharing options...
smerny Posted March 27, 2012 Share Posted March 27, 2012 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>"); }); }); Quote Link to comment https://forums.phpfreaks.com/topic/259823-if-not-working-for-this-noob/#findComment-1331630 Share on other sites More sharing options...
smerny Posted March 27, 2012 Share Posted March 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/259823-if-not-working-for-this-noob/#findComment-1331633 Share on other sites More sharing options...
Who8MyFish Posted March 28, 2012 Author Share Posted March 28, 2012 Thank you for your help, this worked perfectly. I'm wondering why the syntax is different from your typical JavaScript if(this){do this}? http://www.w3schools.com/js/js_if_else.asp Quote Link to comment https://forums.phpfreaks.com/topic/259823-if-not-working-for-this-noob/#findComment-1331952 Share on other sites More sharing options...
smerny Posted March 28, 2012 Share Posted March 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/259823-if-not-working-for-this-noob/#findComment-1331958 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.