"Why?" Because you are failing to understand "variable scope"
Don't use the "var" keyword inside the if block, by using that, you are stomping over the "outside" one http://www.google.com/search?q=Javascript+variable+scope On Sep 2, 10:26 pm, "Rick Faircloth" <r...@whitestonemedia.com> wrote: > Here's the code: > > var valid = 'yes'; > > $(':input').each(function() { > > if ( $(this).val().length == 0 ) > > { alert('in!'); > > var valid = 'no'; > > alert(valid); > > $('#rentalAppErrors').fadeIn(500); > > $(this).addClass('inputError'); > > $(this).css({'background-color':'red','color':'#fff'}); > > $(this).val('Entry required...'); } > > }); > > console.log(valid); > > When I run this code making the "if" statement true, I get the alert('in!') > properly, > > letting me know that I'm inside the conditional block. I also get the > second > > alert, alert(valid), and the value of "valid" in the alert is "no".as it > should be. > > But when the code gets to the console.log(valid) function, valid shows in > the console > > as having the value 'yes'. > > ??? > > Why? This is throwing the rest of my code off. > > Rick > > ---------------------------------------------------------------------------- > --------------------------------------- > > "Those who hammer their guns into plows will plow for those who do not." - > Thomas Jefferson