Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery callbacks messing with variable scopes and functions?
    primarykey
    data
    text
    <p>Good morning people - I've been having this problem for hours and I can't isolate it.</p> <p>I have this piece of jQueryzed JavaScript:</p> <pre> jQuery(document).ready(function() { var validated = 1; jQuery('#help_continue').click(function() { jQuery('#step' + validated + ', #step' + validated + '_help').fadeOut(200, function() { jQuery('#step' + validated + '_help').removeClass('visible').find('.visible').removeClass('visible'); jQuery('#step' + (validated + 1) + '_help').addClass('visible'); jQuery('#step' + (validated + 1) + '_help div:first').addClass('visible').css({display: 'block'}); jQuery('#step' + (validated + 1) + ', #step' + (validated + 1) + '_help').fadeIn(200); }); }); }); </pre> <p>All good, nothing too fancy. If bound to HTML, it works as expected.</p> <p>The thing is that, when I add this to the mix:</p> <pre> jQuery(document).ready(function() { var validated = 1; jQuery('#help_continue').click(function() { jQuery('#step' + validated + ', #step' + validated + '_help').fadeOut(200, function() { jQuery('#step' + validated + '_help').removeClass('visible').find('.visible').removeClass('visible'); jQuery('#step' + (validated + 1) + '_help').addClass('visible'); jQuery('#step' + (validated + 1) + '_help div:first').addClass('visible').css({display: 'block'}); jQuery('#step' + (validated + 1) + ', #step' + (validated + 1) + '_help').fadeIn(200); alert(validated); // this... }); validated++; // ...and this. }); }); </pre> <p>The alert it shown TWICE, and the "validated" variable is NEVER = 1 inside the function - always 2.</p> <p>I'm no JavaScript guru for sure, but I definitely know that that's just plain wrong, unless I'm missing something. I come from a PHP background, and I know that JavaScript has its idiosyncrasies, but this is just weird.</p> <p>I'm using jQuery 1.5 if it matters. Anyone knows what's happening?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload