Calculate percentage in real time

Asked

Viewed 241 times

-2

I wanted to put 2 inputs on my page, 1 the user will put a number( anyone ) and in the box ahead appear the division of this number in 50%, in real time, as I would do it?

  • 1

    Post the code you’ve tried. Tips: Utilize parseInt or parseFloat to convert the value of input of String for Number; Also use the event input in the element input to detect - in real time - changes.

  • Integer or decimal number?

1 answer

0

So in order to do this, you would have to rely on Javascript as well, because HTML is just a markup language and you can’t/can’t make code let’s say, "logical" in your language.

Seen this, basically what you would have to do is take the number entered in the field and divide this number by a supposed "2" (it being half of 1 number):

function dividirNumero() 
{
    num1 = document.getElementById("dividir").value;
    document.getElementById("resultado").value = num1 / 2;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Número: <input type="text" onKeyUp="dividirNumero()" id="dividir" /> | Resultado: <input type="text" id="resultado"/>

This is a very basic concept of how you can do this calculation that you’re looking for, because every number you put up will find half of it. There are more complete codes to achieve this result, but they are more complex.

I suggest you take a look in the documentation of Arithmetic Operators so that you have a broad example of how too many operations can be done with your code! :)

  • What does the library do in the answer? Some user might think they should use it.

  • @Leocaracciolo only if the user goes over all the explanation and does not read anything, because before and until later I say that it is only one REFERRAL for a greater understanding of the subject.

  • I said this one 3.3.1/jquery.min.js is floating there. No use of fact.

Browser other questions tagged

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