webguy08
01-27-2009, 03:15 PM
Hi all,
I decided to start learning JavaScript yesterday and I have come up with the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>JS Practice</title>
</head>
<body>
<form method="post">
<input type="button" value="Add to basket" name="Add to basket" onclick="addToBasket()"/>
</form>
<script type="text/javascript">
var tshirt = 34.99;
var total = 0;
var i = 0;
function addToBasket() {
var clothesArray = new Array(i);
total = total + tshirt;
clothesArray[i] = total;
document.write(clothesArray[i]);
i = i + 1; }
</script>
<script type="text/javascript">
document.write(total);
</script>
</body>
</html>
Whenever I run the addToBasket() function it loads a new page, but I don't want that to happen. I want it to remain on the same page and see the value being increased.
How would I go about doing so?
Thanks for any help :cool:
I decided to start learning JavaScript yesterday and I have come up with the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>JS Practice</title>
</head>
<body>
<form method="post">
<input type="button" value="Add to basket" name="Add to basket" onclick="addToBasket()"/>
</form>
<script type="text/javascript">
var tshirt = 34.99;
var total = 0;
var i = 0;
function addToBasket() {
var clothesArray = new Array(i);
total = total + tshirt;
clothesArray[i] = total;
document.write(clothesArray[i]);
i = i + 1; }
</script>
<script type="text/javascript">
document.write(total);
</script>
</body>
</html>
Whenever I run the addToBasket() function it loads a new page, but I don't want that to happen. I want it to remain on the same page and see the value being increased.
How would I go about doing so?
Thanks for any help :cool: