Question

I have a form with two text inputs. The second input has a number, that should be changed by the value inside the first input.

When the user clicks a button, the second input should be changed (aumented) in the same percentage that the first input says.

This is my code, that isn´t working: when I click the button nothing happens.

Aumentar un <input id="porcentaje">% <input type="button" id="aumentar" value="Aumentar" onclick="calcular()">
<br>Monto: <input id="campo">

This is the calcular() function:

function calcular() {
var original = document.getElementById('campo').value;
var aumento = document.getElementById('porcentaje').value;

original = original*aumento/100;
}

And here is the jsfiddle link to the script.

Please note that I´ve not named "submit" the button, nor used onsubmit() because the form already has one submit button.

Was it helpful?

Solution

Try this: http://jsfiddle.net/NAM7C/

calcular = function calcular() {
var original = document.getElementById('campo');
var aumento = document.getElementById('porcentaje').value;

original.value = (original.value*aumento/100);
}

OTHER TIPS

This is closer to your original code, note that in JSFiddle you should set No wrap - in body for your event handler to work.

http://jsfiddle.net/S9Msj/

function calcular() {
var original = document.getElementById('campo');
var aumento = document.getElementById('porcentaje').value;

original.value = original.value*aumento/100;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top