P. Lau
P. Lau

Reputation: 165

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?

Upvotes: 0

Views: 75

Answers (5)

Debojyoti Chakraborty
Debojyoti Chakraborty

Reputation: 31

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

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

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

Upvotes: 0

Samyappa
Samyappa

Reputation: 531

<!DOCTYPE html>
<html>
<body>

Name: <input type="text" id="name1" value="" onBlur="myFunction(this)"><br>
Age: <input type="number" id="age1" value="" onBlur="myFunction(this)"><br>
Job: <input type="text" id="job1" value="" onBlur="myFunction(this)"> <br>
Tel: <input type="text" id="tel1" value="" onBlur="myFunction(this)"> <br> 

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

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

<script>
function myFunction(arg) {

    var id = arg.getAttribute('id');
    var x = document.getElementById(id).value;

     document.getElementById("demo").innerHTML = x;
}

</script>

</body>
</html>

Upvotes: 1

V&#245; Minh
V&#245; Minh

Reputation: 155

Pass parameter this value to the function.

<body>

Name: <input type="text" id="name1" value="" onBlur="myFunction1(this.value)"><br>
Age: <input type="number" id="age1" 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>

<script>
function myFunction1(val) {

    document.getElementById("demo").innerHTML = val;

}

</script>

but I think what you need is this one.

Name: <input type="text" id="name1" value="" onBlur="myFunction1(this.value)"><br>
Age: <input type="number" id="age1" value="" onBlur="myFunction2(this.value)"><br>


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

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

Age Your input:
<p id="demo1"></p>

<script>
function myFunction1(val) {

    document.getElementById("demo").innerHTML = val;

}

function myFunction2(val) {

    document.getElementById("demo1").innerHTML = val;

}

</script>

Upvotes: 0

Bhargav Chudasama
Bhargav Chudasama

Reputation: 7171

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>

Upvotes: 3

Dalvinder Singh
Dalvinder Singh

Reputation: 261

You can do like this :

<!DOCTYPE html>
<html>
<body>

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

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

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

<script>
function myFunction1(ID) {
    var x = document.getElementById(ID).value;
    document.getElementById("demo").innerHTML = x;
}

</script>

</body>
</html>

Upvotes: 1

Related Questions