Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to use Global Variables while trying to use multiple functions - JavaScript - Beginner
    text
    copied!<p>As total JavaScript beginner I am unable to use global variable while trying to use multiple functions. The code is as follows - </p> <pre><code>&lt;script type="text/javascript"&gt; /* 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(); } &lt;/script&gt; </code></pre> <p>Using as:- </p> <pre><code>&lt;input type="submit" value="Submit" onclick="compute ()"&gt; </code></pre> <p>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.</p> <p>Thanks in advance. :)</p> <p>P.S: First question here. Excuse my mistakes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload