Replace anchor by form button with calculation by javascript

Asked

Viewed 407 times

8

I have the following code:

function calc(){
  var form = document.getElementById("form");
  var a = +form.a.value;
  var b = +form.b.value;
  var result = (a+b);
  form.result.value = result;
}
<form id="form">
  A: <input type="number" name="a">
  B: <input type="number" name="b">
  Result: <input type="text" name="result" disabled="">
  <a href="#" onclick="calc();">Calcular</a>
</form>

I’d like to put in place the tag <a> one <button>, but when doing this the page refreshes right after the calculation and the result is not visible. Here in the post does not update after I click on the button, but on my local page yes.

  • I don’t quite understand what you want...

  • In place of the tag <a> would like to put a <button>, but when doing this the page refreshes right after the calculation and the result is not visible.

  • Try the way I answered there. if not comment...

1 answer

7


I believe that if I understand correctly, just put a button of type not Ubmit, not to send the form and reload the page without you see the result!

function calc(){
	var form = document.getElementById("form");
	var a = +form.a.value;
	var b = +form.b.value;
	var result = (a+b);
	form.result.value = result;
}
<form id="form">
	A: <input type="number" name="a">
	B: <input type="number" name="b">
	Result: <input type="text" name="result" disabled="">
	<input type="button" onclick="calc();" value="Calcular">
</form>

Worth remembering that the operator + used after equal sign, as in the case used: var a = +form.a.value; It is used to indicate the input of a positive number, since returning the value of an input, by default will come as String. I mean, if I didn’t have the +, and simply put (a+b)would be understood as two strings, concatenate instead of adding them.Here is the example below:

function calc(){
	var form = document.getElementById("form");
	var a = form.a.value;
	var b = form.b.value;
	var result = (a+b);
	form.result.value = result;
}
<form id="form">
	A: <input type="number" name="a">
	B: <input type="number" name="b">
	Result: <input type="text" name="result" disabled="">
	<input type="button" onclick="calc();" value="Calcular">
</form>

In short:

var  um  =  "1" ; 
var  b  =  um ;      // B = "1": uma string 
var  c  =  + um ;     // C = 1: um número 
var  d  =  - um ;     // d = -1: um número

And another way to get the same result would be to "convert" the value from the input, using Eval:

function calc(){
	var form = document.getElementById("form");
	var a = form.a.value;
	var b = form.b.value;
	var result = (eval(a)+eval(b));
	form.result.value = result;
}
<form id="form">
	A: <input type="number" name="a">
	B: <input type="number" name="b">
	Result: <input type="text" name="result" disabled="">
	<input type="button" onclick="calc();" value="Calcular">
</form>

  • That’s right! Thank you!

  • 2

    You can put together an explanation for what the + do here +form.a.value? It’s a practical trick but it can get a lot of people confused.

  • @Sergio, in this way, I was able to exemplify well?

  • 1

    @Localhost turned excellent, almost a new response :) +1

  • 1

    @Laerciolopes, if you used parseInt(form.a.value) you would be converting to an integer number, limited to integer number, with +form.a.value you can use negative numbers, just type in the inputs -3 and etc...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.