i am try to use javascript to count up like 1, 2, 3 … but only when a button is clicked. so each click add 1 so if the buttons been clicked 10 it should disply the number 10.
(it is more like a Download/Click Counter)
how would i do this???
lol I don’t know javascript but just use this pseudo:
name.textfield=0
if counter.Button = push
name.textfield= name.textfield+1
lol
<!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" lang="en" xml:lang="en">
<head>
<title> </title>
<script type="text/javascript">
var counter = 0;
function countUp(){
counter++;
document.getElementById('countUp').innerHTML = counter;
} </script>
</head>
<body>
<div id="counter">
<span id="countUp">0</span><br />
<input type="button" name="cButton" value="Count Up" onclick="countUp()" />
</div>
</body>
</html>
It’s pretty simple really. Make sure to declare the counter variable outside of your function. Then create a function that will increase the counter variable each time the button is clicked and display it. For the button you’re just using onclick to call the function.
I didn’t know where you wanted the number displayed so I just tossed it into a span tag using getElementById(). You could just as easily use a form and text field. Like so:
<!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" lang="en" xml:lang="en">
<head>
<title> </title>
<script type="text/javascript">
var counter = 0;
function countUp(){
counter++;
document.counterForm.displayCounter.value = counter;
} </script>
</head>
<body>
<form name="counterForm">
<input type="text" name="displayCounter" value="" /> <br />
<input type="button" name="cButton" value="Count Up" onclick="countUp()" />
</form>
</body>
</html>