I have a global variable ‘input’ and it’s value is assigned in the function
processinput
. But if I try to access the value input
inside a different function, the value is displayed undefined.
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Visualization</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.plot.ly/plotly-1.58.5.min.js"></script>
<style>
/* Your CSS styles here */
</style>
</head>
<body>
<div class="graph-container">
<div id="network" class="main-panel"></div>
<div id="graph" class="side-panel">
<div class="arrow"></div>
</div>
</div>
<label for="quantity">Enter the value</label>
<input type="text" id="quantity">
<button id="submit" onclick="processInput()">Submit</button>
<script>
var input;
function processInput() {
input = document.getElementById("quantity").value;
console.log(input);
}
document.addEventListener("DOMContentLoaded", function() {
// Rest of your code...
console.log(input);
// Fetch JSON data, initialize graph, and set up event listeners
});
</script>
</body>
</html>
Suggestions on how to access the value of input
outside processInput
function will be really helpful.
EDIT:
Thanks for the comments. For instance, if I try to display the value of input
outside the processInput function, it is undefined.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Visualization</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.plot.ly/plotly-1.58.5.min.js"></script>
<style>
/* Your CSS styles here */
</style>
</head>
<body>
<div class="graph-container">
<div id="network" class="main-panel"></div>
<div id="graph" class="side-panel">
<div class="arrow"></div>
</div>
</div>
<label for="quantity">Enter the value</label>
<input type="text" id="quantity">
<button id="submit" onclick="processInput()">Submit</button>
<script>
var input;
function processInput() {
input = document.getElementById("quantity").value;
console.log(input);
}
function test() {
// Rest of your code...
console.log(input);
// Fetch JSON data, initialize graph, and set up event listeners
};
console.log(input);
</script>
</body>
</html>
2
Answers
Did you try to give a value to var input ?
I think it doesn’t work because you don’t give a value, you only give a value in the processInput function.
Your global input variable has no value on page load. And it will never have a value just after you assign something to it. Which you do but in your
processInput()
function which only called on submit button click.Javascript runs as soon as you load the page and it will print the undefined global input variable before you even call the
processInput()
function.I suggest that you assign a value as soon as the javascript runs like this:
In your process input function you can assign a value to an other variable if you want to, something like this: