Answer the question
In order to leave comments, you need to log in
Shared variables in multiple functions?
Hey!
How can you take a variable from the first function and use it in the second?
It is important that if you enter numbers in the input, they are instantly shown and the calculation takes place.
ps sandbox jsFiddle For example, how to display the product of variables from two functions in info5
In my example, 2 separate functions:
1 function - takes data-* values in option from select. Assigns to variables and outputs span elements.
<select onchange="costCalculator()" id="card">
<option value="0">Выберите Ваше оборудование</option>
<option data-power="9" data-quantity="3" value="100">GeForce</option>
<option data-power="7" data-quantity="7" value="200">nVidia</option>
<option data-power="6" data-quantity="333" value="300">AMD</option>
</select>
<script>
function costCalculator(select) {
var option = select.options[select.selectedIndex];
document.querySelector('#info2').innerHTML = option.text;
var power = option.getAttribute('data-power');
document.querySelector('#info3').innerHTML = power;
var quantity = option.getAttribute('data-quantity');
document.querySelector('#info4').innerHTML = quantity;
var value = option.getAttribute('value');
document.querySelector('#info1').innerHTML = value;
}
document.querySelector('#card').onchange = function() {
costCalculator(this);
}
</script>
<input type="text" id="rashod" value="1" oninput="calc()" />
<script>
function calc() {
var rashod = document.getElementById("rashod");
info5.innerHTML = parseInt(rashod.value);
}
</script>
var x = {};
function(select) {
.....
x.lala = power.value;
}
function(calc) {
var y = x.lala;
var rashod = document.getElementById("rashod");
info5.innerHTML = parseInt(rashod.value) + y;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question