I am adding two text boxes using function and want to display result in third text box. For this I used two functions. First getdata()
to read first textbox value and second is getdata1()
to read second textbox value. I also use onchange event. but in result it display NaN
. Please, help me.
Here is my Code
var a, b, c;
function getdata(txt) {
x = txt.value;
if (!isNaN(x)) {
a = parseInt(document.getElementById("txt").value);
} else {
alert("Input not valid");
txt.focus();
txt.value = "";
}
}
function getdata1(txt) {
x = txt.value;
if (!isNaN(x)) {
b = parseInt(document.getElementById("txt").value);
} else {
alert("Input not valid");
txt.focus();
txt.value = "";
}
}
function myFunction(s, t) {
var s = a;
var t = b;
var c = s + t;
document.getElementById("result").value = c;
}
<td><input type="text" name="txt[]" id="txt[]" onchange="getdata(this)" /></td>
<td><input type="text" name="txt[]" id="txt[]" onchange="getdata1(this)" /></td>
<td><input type="text" name="result" id="result" onfocus="myFunction()" /></td>
See Question&Answers more detail:os