Change value by text

Asked

Viewed 30 times

0

I would like to know if it is possible to exchange the value of the imput sums for text. Ex: if the result is greater than 100, the value is exchanged for a text.

Ex: If the result is greater than 100 = GOOD

<html>
   <body>
<script> 
function soma() 
{
form.campo4.value = (form.campo1.value*1) + (form.campo2.value*1) + (form.campo3.value*1)
}

</script>
<form name="form">
<input name="campo1"><br> 
<input name="campo2"><br> 
<input name="campo3"><br> 
<input name="campo4" readonly><br>
<input type="button" onclick="soma()" value="Soma os Valores">
</form>
   </body>
</html>

if you could leave as it would the code already changed I would appreciate :)

1 answer

1


You can do it this way:

function soma(){
   var resultado = +form.campo1.value + +form.campo2.value/2 + +form.campo3.value;
   var texto = 'RUIM';
   if(resultado > 100){
      texto = "BOM";
   }
   form.campo4.value = texto;
}
<form name="form">
   <input name="campo1"><br> 
   <input name="campo2"><br> 
   <input name="campo3"><br> 
   <input name="campo4" readonly><br>
   <input type="button" onclick="soma()" value="Soma os Valores">
</form>

No need to multiply the values by 1 to be able to convert to number. Just add a + before it becomes number.

See that I put a standard text RUIM and if the result is greater than 100, this text BOM.

Browser other questions tagged

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