1

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 :/

<head>
<script type="text/javascript">
var globalch = 1;
function chk()
{
if(chicken){
globalch = 3;
}else{
globalch = 3;
}
}
</script>
</head>
<body>
<a href="#" onclick="alert(globalch)">What is the variable?</a><br />
<a href="#" onclick="chk">Change the variable to 3</a>
</body>

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!


EDIT:

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 checkdetails(); works perfectly fine, but when it tries to change the global variable done, nothing happens. Here is the script:

<script type="text/javascript">
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();
}
}

and here is the object that calls the function:

<a href="#" id="paypalpay" onclick="payp()">
<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" />
</a>

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 done is not changing. Thanks for all of your help so far, can anyone spot where on earth I'm going wrong here?

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
matthewc
  • 69
  • 9

2 Answers2

3

Your call "chk" isn't executing the function at all. You need to call it by adding ()

<a href="#" onclick="chk()"
                         ^ needed to execute function

Also, chicken isn't defined, so it throws an error, after fixing that.

Uncaught ReferenceError: chicken is not defined 

(JSFiddle)


I realised the scope of an if statement in a function is different to the scope of a function

That is not true. The scope of the function applies to all blocks within it. JS does not have block-scope.


EDIT: Part 2 of your question

if(done="false")
       ^ should be ==
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • I think it was the fact that chicken wasn't defined. Which leads to me not understanding why it doesn't work on my far larger script. Should I post this instead?I'm not sure why I posted this code without the chk() being called in the onclick, as in my script it was being called. – matthewc Nov 16 '12 at 08:48
  • You can edit your question or give a link to [**JSFiddle**](http://jsfiddle.net) if it is too long. – Anirudh Ramanathan Nov 16 '12 at 08:50
  • And thanks for the help, that certainly fixed this problem. I now know I'm not mistaken in my knowledge of scope – matthewc Nov 16 '12 at 08:51
  • @user1829007 If you have any questions regarding scope, [**these answers**](http://stackoverflow.com/questions/500431/javascript-variable-scope) should resolve it. – Anirudh Ramanathan Nov 16 '12 at 08:54
  • I understand that this isn't a scope issue now. Whilst looking for a problem, I was searching for a resolution to variable scope issues, and I've now realised that I've been looking at the wrong problem thanks to your answers! – matthewc Nov 16 '12 at 09:06
  • @matthewc You are assigning "false" to `done`, in your code, instead of comparing using `==`. – Anirudh Ramanathan Nov 16 '12 at 09:27
  • You are an absolute star. It also turns out that the variable done might also be system variable, as it didn't work until I changed the name of it to "done1". This is the final bug ironed out of a very large bespoke e-commerce system, so I really appreciate the brilliant help! – matthewc Nov 16 '12 at 10:14
  • Thank you Cthulhu. Fantastic person :D – matthewc Nov 16 '12 at 10:17
0

I think you should have parenthesis after the function name inside the onclick attribute:

<a href="#" onclick="chk()">Change the variable to 3</a>
niaher
  • 9,460
  • 7
  • 67
  • 86