javascript - Plus Arithmetic Operation -


i've tried use plus arithmetic operation calculate 2 input text type number values, result values "together", that:

  • value one: 5
  • value two: 5

result

55 

wanted result

10 

code

<script type="text/javascript">    function cal(){     var plus = document.getelementbyid('plus').value,         plus1 = document.getelementbyid('plus1').value;     var x = plus + plus1;     var result = document.getelementbyid('result');     if(result.value == ""){      result.innerhtml = "?";     }     else{      result.innerhtml = x;     }   } </script>  <input type="text" id="plus" /> + <input type="text" id="plus1" /> = <span id="result"></span> 

how can plus result instead couple of numbers together?

you need parse them integer type:

var x = parseint(plus) + parseint(plus1); 

then can use isnan() determine whether valid operation or not: (note i'm checking value of x not of resulting output)

if(isnan(x)){  result.innerhtml = "?"; } else{  result.innerhtml = x; } 

living demo: http://jsfiddle.net/4kb5y/3/


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -