SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: A simple counter routine
-
Apr 3, 2001, 06:19 #1
- Join Date
- Dec 2000
- Location
- Norcross, GA
- Posts
- 136
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The idea here is the counter gets updated every time a user checks or unchecks a checkbox.
Obviously the way it's coded now only a 0 modulus will be returned. How can I init the variable count without setting it to zero every time the function is called?
Code:function changeit() { var count = 0; count = count++; if (count%2 == 0) { document.forms[0].transmissionfirst.value = "00/00/0000"; document.forms[0].transmissionlast.value = "00/00/0000"; } else { document.forms[0].transmissionfirst.value = ""; document.forms[0].transmissionlast.value = ""; } }
- Mike
http://www.georgiaoffroad.com
-
Apr 3, 2001, 07:03 #2
- Join Date
- Feb 2001
- Location
- Van down by the river
- Posts
- 254
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm not sure exactly what you're trying to do, but it sounds like you don't want the variable to restart at zero everytime the function is called? If so, you could store it in a hidden form field, since it looks like you're using a form anyway. When the function is called again, retrieve the hidden value and do as you please with it.
-
Apr 3, 2001, 07:32 #3
- Join Date
- Dec 2000
- Location
- Norcross, GA
- Posts
- 136
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
There is a form with a textbox and checkbox.
The textbox is blank by default.
When the user clicks the checkbox, the textbox is filled with $string (this part works fine, btw).
This is where I am - I need to be able to clear the textbox if the user clicks the checkbox a second time. And if it is clicked a third time, it will place $string in the textbox again, and so on...- Mike
http://www.georgiaoffroad.com
-
Apr 3, 2001, 08:29 #4
- Join Date
- Feb 2001
- Location
- Van down by the river
- Posts
- 254
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Here's some code that should work. It increments the count each time - not sure if you wanted to do that or not. If not, it can be easily changed.
<html>
<head>
<title>Untitled</title>
<script language="JavaScript">
<!--
function countIt() {
frm = document.myForm;
if (frm.addCount.checked)
{ frm.displayCount.value = parseInt(frm.theCount.value);
frm.theCount.value = parseInt(frm.theCount.value) + 1;
} else {
frm.displayCount.value = '';
}
}
// -->
</script>
</head>
<body>
<form name="myForm" method="post" action="">
<input type="checkbox" name="addCount" onClick="countIt();">Add to count<br>
<input type="text" name="displayCount"><br>
<input type="hidden" name="theCount" value="1">
</form>
</body>
</html>
-
Apr 3, 2001, 08:54 #5
- Join Date
- Dec 2000
- Location
- Norcross, GA
- Posts
- 136
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
your script works fine, though it is not quite what I'm after.
The only reason I have the count routine in my script is to toggle on and off the $string variable, which will always remain the same (00/00/0000).
Code:$count is initially set to zero. When the user clicks the checkbox { $count = $count + 1; if $count%2 == 0 { '00/00/0000' will appear in the textbox } else if $count%2 == 1 { textbox is cleared, or given a value of ''; } }
Last edited by mstembri; Apr 3, 2001 at 09:04.
- Mike
http://www.georgiaoffroad.com
-
Apr 3, 2001, 09:21 #6
- Join Date
- Feb 2001
- Location
- Van down by the river
- Posts
- 254
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
In that case, use the hidden variable I mentioned to store the value of the count. Replace these two lines in your original script:
var count = 0;
count = count++;
with this:
var count = parseInt(document.forms[0].theCount.value) + 1;
where 'theCount' is the name of the hidden form field that remembers the count.
So, count increments each time the checkbox is checked or unchecked, and if count is evenly divisible by 2, 00/00/0000 will appear in the textbox.
Do I have a grip yet?
-
Apr 3, 2001, 10:40 #7
- Join Date
- Dec 2000
- Location
- Norcross, GA
- Posts
- 136
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try this:
http://www.georgiaoffroad.com/www.html
The counter isn't being updated...- Mike
http://www.georgiaoffroad.com
-
Apr 3, 2001, 11:04 #8
- Join Date
- Feb 2001
- Location
- Van down by the river
- Posts
- 254
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
My fault.
Here are the first two lines for the script:
document.forms[0].theCount.value = parseInt(document.forms[0].theCount.value) + 1;
var count = parseInt(document.forms[0].theCount.value) + 1;
So first we increment the counter, then assign the variable 'count' to that value.
You've got the second line in there, but it's commented, so make sure you remove the comment. This should work for you.
Bookmarks