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?