Q:

Single Function To Capture Different Value Of Different Input Field

For the code below, I have to use 4 functions to display value from 4 input field.

<!DOCTYPE html>
<html>
<body>

Name: <input type="text" id="name1" value="" onBlur="myFunction1()"><br>
Age: <input type="number" id="age1" value="" onBlur="myFunction2()"><br>
Job: <input type="text" id="job1" value="" onBlur="myFunction3()"> <br>
Tel: <input type="text" id="tel1" value="" onBlur="myFunction4()"> <br>

<p>Type then click outside to display the value of the input field.</p>

Your input:
<p id="demo"></p>

<script>
function myFunction1() {
    var x = document.getElementById("name1").value;
    document.getElementById("demo").innerHTML = x;
}
function myFunction2() {
    var y = document.getElementById("age1").value;
    document.getElementById("demo").innerHTML = y;
}
function myFunction3() {
    var z = document.getElementById("job1").value;
    document.getElementById("demo").innerHTML = z;
}
function myFunction4() {
    var a = document.getElementById("tel1").value;
    document.getElementById("demo").innerHTML = a;
}
</script>

</body>
</html>

Is there a way to use just 1 function to do the same work?

A:

try this one

function myFunction1(val) {
  var x = val;
  document.getElementById("demo").innerHTML = x;
}
Name: <input type="text" id="name1" value="" onBlur="myFunction1(this.value)"><br>
Age: <input type="number" id="age1" value="" onBlur="myFunction1(this.value)"><br>
Job: <input type="text" id="job1" value="" onBlur="myFunction1(this.value)"> <br>
Tel: <input type="text" id="tel1" value="" onBlur="myFunction1(this.value)"> <br>

<p>Type then click outside to display the value of the input field.</p>

Your input:
<p id="demo"></p>

