> you are stomping over the "outside" one Hmmm...that's what I'm trying to do.
I assume you mean by the "'outside' one", the variable, "valid", inside "console.log(valid)". I'm initially setting the variable "valid" to "yes" and using the conditional block to change "valid" to "no" if there's an invalid entry in any of the text fields. Would this not be a proper way to proceed? --Original Message----- From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On Behalf Of MorningZ Sent: Wednesday, September 02, 2009 10:36 PM To: jQuery (English) Subject: [jQuery] Re: Why is console.log(valid) returning "yes" in this code? "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