Subtract percentage in real time in an HTML with JavaScript, or by calling a PHP

1

It is to make a simple form where we put the data of a monthly expense in number. The idea is that when doing click in calculating savings show you again your spending and the% saving 8% . I need to know if with AngularJs or with php or javascript that a data that includes the user in a input shows the result below with a reduction of 8% of the percentage.

It's for my website, a simple calculator.

Already with CSS I will enter that style.

Could you help me?

Visual attachment of the form that they ask me:

    
asked by Walworth Industrial de Vlvulas 02.12.2017 в 00:11
source

2 answers

2

You can try the following:

function calcular(){
  //Obtienes el valor
  var valor = document.getElementById("valor").value;

  var result= document.getElementById('result');
  var total= document.getElementById('total');

  //le descuentas el 8% y lo agregas al HTML
  var descuento = parseInt(valor)*0.08;

  //agrega los resultados al DOM
  result.innerHTML = 'Ahorro de: $' + descuento;
  total.innerHTML =  'Total:     $' + (parseInt(valor)-descuento);
}
Ingrese un valor:
<!--Con el evento "onkeyUp" haces el calculo a medida que ingresan el valor-->
<input id="valor" type="number" onkeyUp="calcular();">
<br>
<br>
<span id="result"></span>
<br>
<span id="total"></span>
    
answered by 02.12.2017 / 00:20
source
0

You can get the value of the input with javascript

let gastoInput = document.getElementById('gasto');
let gasto = gastoInput.val();

You calculate the savings

let ahorro = (gasto * 100) / 8;

You print the saving in HTML with a method like append or innerHTML.

    
answered by 02.12.2017 в 00:18