Question

Hello almighty internetz!

Im totally new on javascript, and i cant get the script to fetch numbers from input boxes, plus the sums together and write it out.

Have been sitting on Google for an hour now, so im asking you guys/girls for help!

http://jsfiddle.net/heWM2/5/

<form action="" id="brod">
    <p>Pris per kartong
        <input name="Prisperkartong" type="number" id="priskart" name="pris">
    </p>
    <p>Antal kartonger i leverans
        <input name="Kartongilev" type="number" id="kartilev">
    </p>
    <input type="button" onClick="calculateTotal()" value="Räkna">
    <div id="print"></div>
</form>

And the Javascript:

function getPrisperkartong() {
var Prisperkartong = parseInt(document.brod.priskart.value, 10);
if (isNaN(Prisperkartong)) return;
document.bord.priskart.value = Prisperkartong;
}

function getKartongilev() {
var Kartongilev = parseInt(document.brod.kartilev.value, 10);
if (isNaN(Kartongilev)) return;
document.bord.kartilev.value = Kartongilev;
}
function calculateTotal() {
var total = getPrisperkartong() + getKartongilev();

var divobj = document.getElementById('print');
divobj.style.display = 'block';
divobj.innerHTML = "Pris $" + total;
}
Was it helpful?

Solution

If you are accessing the input boxes through the form, you need to use the name field to identify the form as well as the input boxes. Your get functions should also return the value, not assign it back to the original input box. See this updated fiddle for a fixed, working example.

http://jsfiddle.net/heWM2/6/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top