Skip to content Skip to sidebar Skip to footer

Call A Global Variable Into Two Functions Using JavaScript

I have a global variable: var x = document.getElementById('Node').value; Where the value is determined by a text box with the ID of 'Node'. I have two functions that don't same th

Solution 1:

You can simply use:

window.x = document.getElementById("Node").value;

Solution 2:

Try this

(function() {
    var x = document.getElementById("Node").value;
    document.getElementById("Node").onchange = function() {
      myNode()
    };
    document.getElementById("mySelect1").onchange = function() {
      myNotes()
    };
    document.getElementById("mySelect2").onclick = function() {
      summary()
    };
    
     function myNode() {
      x = document.getElementById("Node").value;
    }

    function summary() {
      var a = document.getElementById("mySelect1").value;
      var b = document.getElementById("mySelect2").value;
      document.getElementById("5").value = a + " - " + b + " - " + x;
    }

    function myNotes() {

      var a = document.getElementById("mySelect1").value;
      var b = document.getElementById("mySelect2").value;
      document.getElementById("4").value = a + " + " + b + " + " + x;
    }


  }





)();
Notes : <input type="text" id="4" /><br> Summary : <input type="text" id="5" /><br>
<select id="Node">
  <option value="w">w
  <option value="x">x
  <option value="y">y
  <option value="z">z
</select>
<select id="mySelect2">
  <option value="a">a
  <option value="b">b
  <option value="c">c
  <option value="d">d
</select>
<select id="mySelect1">
  <option value="1">1
  <option value="2">2
  <option value="3">3
  <option value="4">4
</select>

Post a Comment for "Call A Global Variable Into Two Functions Using JavaScript"