Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8898967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:44:39+00:00 2026-06-15T00:44:39+00:00

Original Question: I’ve been having a major headache over what should be an extremely

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:44:40+00:00Added an answer on June 15, 2026 at 12:44 am

    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 ==
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Original question has been answered. Update addresses related question raised in comments. Original post:
Original Question Is there any way to read a HTTPOnly 1 cookie with JavaScript?
Original Question: Hello, I am creating very simple hobby project - browser based multiplayer
(I rewrite the original question. The problem is the same.) The above example code
Edit: Below is my original question. After solving my problem, I thought I'd re-edit
UPDATED QUESTION WITH ANSWER!! Original Question I've been looking at Eli's object.watch (https://gist.github.com/384583) script
Original question I met a strange problem with nginx + php-fpm. If my root
The original question is the following: Where A=1 AND A=2 returns 0 row problem
Original question: this bit of javascript code will convert centimeters to feet. But the
My original question: I'm creating a simple drawing application and need to be able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.