I’ve got a function like this (value is from select):
function getSelectValuePower () {
var power = document.getElementById("power").value;
sessionStorage.setItem("power", power);
console.log(power);
}
How can I use value from variable power in another function ?
For example :
I want to use it here :
function getValue () {
var number = 10;
var selectedValue = number + power;
console.log(selectedValue2);
}
So, when my power is 7, the result in getValue() should be 17.
I tried to do it with global variables, but doesn’t work.
3
Answers
Get power in second function:
you can also use
parseInt()
as wellYou can do it by declaring power outside the function or by creating a class. Here’s how you can do it.
Using global declaration:
Using a class: