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);
}
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.
La 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);
}
Autres conseils
This is closer to your original code, note that in JSFiddle you should set No wrap - in body for your event handler to work.
function calcular() {
var original = document.getElementById('campo');
var aumento = document.getElementById('porcentaje').value;
original.value = original.value*aumento/100;
}