javascript - Need help in java script code -
function addnumbers() { var val1 = parseint(document.getelementbyid("value1").value); var val2 = parseint(document.getelementbyid("value2").value); var ansd = document.getelementbyid("answer"); ansd.value = val1 + val2; }
i want show output without click on button please me out.
you can use onchange
, trigger function, once input
blurred. or can use onkeyup
, triggered type.
function addnumbers() { var val1 = parseint(document.getelementbyid("value1").value); var val2 = parseint(document.getelementbyid("value2").value); var ansd = document.getelementbyid("answer"); ansd.value = val1 + val2; } window.onload = function() { addnumbers(); };
<input id="value1" onkeyup='addnumbers()' value="10"/> <input id="value2" onkeyup='addnumbers()' value="20"/> <input id="answer" />
edit:
you can check if value
in input
exists using conditions(if needed) if don't want display nan
when 1 value empty.
Comments
Post a Comment