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 7998331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:11:21+00:00 2026-06-04T15:11:21+00:00

As total JavaScript beginner I am unable to use global variable while trying to

  • 0

As total JavaScript beginner I am unable to use global variable while trying to use multiple functions. The code is as follows –

<script type="text/javascript">

        /* Global Variable example! Not working as one function called onClick. */

        function make_name_variable () { var y_name = document.getElementById('y_name').value; }
        function make_date_variable () { var y_date = document.getElementById('y_date').value; }
        function make_month_variable () { var y_month = document.getElementById('y_month').value; }
        function make_year_variable () { var y_year = document.getElementById('y_year').value; }


        function test(){
                    /*var y_name = document.getElementById('y_name').value;
                    var y_date = document.getElementById('y_date').value;
                    var y_month = document.getElementById('y_month').value;
                    var y_year = document.getElementById('y_year').value;*/

                    document.getElementById('Result').innerHTML = y_date + y_month +  y_year;


                    return true;
            }

        function compute () {

            make_name_variable ();
            make_date_variable ();
            make_month_variable ();
            make_year_variable ();
            test(); 
           }
    </script>

Using as:-

<input type="submit" value="Submit" onclick="compute ()">

Unable to get the result as expected. I want to use the data through out the page so wanted to keep it global for all functions to use. I was not able to set it global in the conventional way as well. Declaring the variable within function gives desired result.
As you must have a realized I am a complete noob so if there are other ways to get the things done please enlighten me. Somehow I feel there must be a better and easier way to solve this.

Thanks in advance. 🙂

P.S: First question here. Excuse my mistakes.

  • 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-04T15:11:22+00:00Added an answer on June 4, 2026 at 3:11 pm

    The var keyword declares a variable in the current scope. If you declare a var inside a function, it will only exist there. The MDN docs are generally very nice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP code: if($billing_total>$limit_to_send){ echo '<script type=text/javascript> window.onload = function() { alert(Sorry,
I am a total beginner with Javascript and JQuery, and am trying to achieve
I have a form that calculates a total number using Javascript. The code for
my rails applications (all 2.3.5) use a total mix of inline javascript, rjs, prototype
I'm a total javascript noobie. I developed the code bellow following and modifing some
I'm trying to get the total height of a page using JavaScript so I
I'm a total beginner to JavaScript, jQuery, and everything else, so when I saw
so I am a total php/javascript noob. I'm trying load a full php array
I have a javascript function to calculate total amount : <script> function calculate(v) {
I'm trying to dynamically calculate the total of an order using Django/Python and Javascript.

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.