M
M
Mesuti2018-02-08 22:15:48
JavaScript
Mesuti, 2018-02-08 22:15:48

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.

The code
<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>


2 function - Takes values ​​from input and instantly displays also in span
The code
<input type="text" id="rashod" value="1" oninput="calc()" />

<script>
function calc() {
var rashod = document.getElementById("rashod");
info5.innerHTML = parseInt(rashod.value); 
    }
</script>


I tried to take out a variable.
Type
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; 
}

But info5 outputs NaN.
This is such a difficult task that even the question on it sounds incomprehensible (

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2018-02-08
@Mesuti

https://jsfiddle.net/0arbcypx/3/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question