
December 8th, 2012, 10:29 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 53
Time spent in forums: 10 h 3 m 27 sec
Reputation Power: 1
|
|
|
How would you guys avoid refreshing the page after JavaScript code is excuted.
Okay I have this code for a price calculator down below.
Every time I click each price, the prices add up to the total amount and it's okay.
However, when I have list of so many product that users would have to scroll down the page, they would be sent back to the top of the page after clicking "Add $xx" and I don't like this. Users may get tired of going back down and up while adding up products' prices. I want the user to stay at the same place after clicking "Add $xx" .
So does anyone know how to solve this problem?
Actually, why does this happen in the first place??
Code:
<script>
function total(amount)
{
var total = document.getElementById("total_storage").value;
total = parseInt(total) + parseInt(amount);
document.getElementById("total_storage").value="";
document.getElementById("total_storage").value = total;
document.getElementById("total_display").innerHTML = total;
}
function resetAll()
{
document.getElementById("total_storage").value = "0";
document.getElementById("total_display").innerHTML="0";
}
</script>
Total Price: $ <span id="total_display">0</span> || <a href="#" onclick="resetAll();return false">Clear Cart</a>
<br/>
<br/>
<input type="button" value="Add $10" onclick="total('10')"/>
<input type="button" value="Add $25" onclick="total('25')"/>
<input type="button" value="Add $100" onclick="total('100')"/>
<input type="button" value="Add $10" onclick="total('10')"/>
<input type="button" value="Add $25" onclick="total('25')"/>
<input type="button" value="Add $100" onclick="total('100')"/>
<input type="button" value="Add $10" onclick="total('10')"/>
<input type="button" value="Add $25" onclick="total('25')"/>
<input type="button" value="Add $100" onclick="total('100')"/>
<input type="button" value="Add $10" onclick="total('10')"/>
<input type="button" value="Add $25" onclick="total('25')"/>
<input type="button" value="Add $100" onclick="total('100')"/>
<input type="button" value="Add $10" onclick="total('10')"/>
<input type="button" value="Add $25" onclick="total('25')"/>
<input type="button" value="Add $100" onclick="total('100')"/>
<input id="total_storage" type="hidden" value="0"/>
|