title: Javascript global variables within an if statement within a function -- looked everywhere?
body: <p>Original Question: I've been having a major headache over what should be an extremely simple JavaScript problem, I've looked for a solution but everyone is doing the same thing as me, except their script seems to be working. I realised the scope of an if statement in a function is different to the scope of a function, but I can't seem to figure it out, nor can I find a solution :/</p>
  
  <pre><code>&lt;head&gt;
  &lt;script type="text/javascript"&gt;
  var globalch = 1;
  function chk()
  {
  if(chicken){
  globalch = 3;
  }else{
  globalch = 3;
  }
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;a href="#" onclick="alert(globalch)"&gt;What is the variable?&lt;/a&gt;&lt;br /&gt;
  &lt;a href="#" onclick="chk"&gt;Change the variable to 3&lt;/a&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>This is meant to do exactly as it says, but it doesn't. The if statement does the same thing for either condition, so it is no problem with that. I've also tried using window.globalch, window['globalch'], and some other suggestions, but to no avail. What am I missing? I'm sure that it's something obvious and it's going to be one of them "Oh yeah!" moments.. Thanks people!</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I've now edited this question seeing as I realized this is not a scope issue. This is now far deeper. For some reason, the function <strong>checkdetails();</strong> works <strong>perfectly fine</strong>, but <strong>when it tries to change the global variable done, nothing happens</strong>. Here is the script:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  var done = "false";
  function checkdetails(done){
  var name = document.getElementById("name").value;
  var address = document.getElementById("address").value;
  var postcode = document.getElementById("postcode").value;
  var city = document.getElementById("city").value;
  var number = document.getElementById("number").value;
  var email = document.getElementById("email").value;
  if(name==""){
  noty({text: "You need to enter your name."});
  }else if(!address){
  noty({text: "You need to enter your address."});
  }else if(!city){
  noty({text: "You need to enter your city."});
  }else if(!postcode){
  noty({text: "You need to enter your postcode."});
  }else if(!number){
  noty({text: "You need to enter your contact number."});
  }else if(!email){
  noty({text: "You need to enter your email address."});
  } else {
  done="true";
  noty({text: "Your details have been submitted!"});
  }
  
  }
  function payp(done){
  if(done="false"){
  noty({text: 'Please submit your details before proceeding to pay.'});
  } else {
  document.goandpay.submit();
  }
  }
  </code></pre>
  
  <p>and here is the object that calls the function:</p>
  
  <pre><code>&lt;a href="#" id="paypalpay" onclick="payp()"&gt;
  &lt;img src="images/paypalpay.png" style="margin-bottom:5px" alt="Proceed to the paypal payment gateway" name="Image1" width="265" height="53" border="0" id="Image1" /&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>Both functions call perfectly fine, the form checking works, the notification comes up when everything is "submitted", the single problem is the fact that the variable <strong>done is not changing</strong>. Thanks for all of your help so far, can anyone spot where on earth I'm going wrong here?</p>