473,624 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counter Script

This simple script counts up or down with a button click, now can it be
modified so the count won't go below zero?
********** Thanks, Joe

<form>
<input type=text name=amount size=4 value=>
<input type=button value="up"
onClick="javasc ript:this.form. amount.value++; ">
<input type=button value="down"
onClick="javasc ript:this.form. amount.value--;">
</form>

Sep 30 '05 #1
22 2590

Papajo wrote:
This simple script counts up or down with a button click, now can it be
modified so the count won't go below zero?
Thanks, Joe

<form>
<input type=text name=amount size=4 value=>
<input type=button value="up"
onClick="javasc ript:this.form. amount.value++; ">
<input type=button value="down"
onClick="javasc ript:this.form. amount.value--;">
</form>


There is no need for the javascript protocol. I prefer not to do
inline javascript unless I can't help it.

Place script in between the <head> tags and you can something like the
following:

<script type = "text/javascript">
function countUp(obj)
{
obj.form.amount .value++;
}

function countDown(obj)
{
amtObj = obj.form.amount ;

if(amtObj.value > 1)
{
obj.form.amount .value--;
}
}
</script>

As for your form:

<form>
<input type = "text" name = "amount">
<input type = "button" value = "up" onClick = "countUp(this); ">
<input type = "button" value = "down" onClick = "countDown(this );">
</form>

Sep 30 '05 #2
Lee
Papajo said:

This simple script counts up or down with a button click, now can it be
modified so the count won't go below zero?
=A0=A0=A0=A0=A 0=A0=A0=A0=A0=A 0 Thanks, Joe

<form>
<input type=3Dtext name=3Damount size=3D4 value=3D>
<input type=3Dbutton value=3D"up"
onClick=3D"jav ascript:this.fo rm.amount.value ++;">
<input type=3Dbutton value=3D"down"
onClick=3D"jav ascript:this.fo rm.amount.value--;">
</form>


<form>
<input type="text" name="amount" size="4" value="0">
<input type="button" value="up"
onClick="this.f orm.amount.valu e++;">
<input type="button" value="down"
onClick="if(thi s.form.amount.v alue>'0')this.f orm.amount.valu e--;">
</form>

Sep 30 '05 #3
Lee wrote on 30 sep 2005 in comp.lang.javas cript:
Papajo said:

<form>
<input type="text" name="amount" size="4" value="0">
<input type="button" value="up"
onClick="this.f orm.amount.valu e++;">
<input type="button" value="down"
onClick="if(thi s.form.amount.v alue>'0')this.f orm.amount.valu e--;">
</form>


this.form.amoun t.value>'0'
the parentheses are unnecessary.
Much better to disable the down button when 0 is reached:

=============== =========
<form>

<input type="text" name="amount" size="4" value="2">

<input type="button" value="up"
onClick="this.f orm.amount.valu e++;
document.getEle mentById('down' ).disabled=fals e">

<input type="button" value="down" id='down'
onClick="this.d isabled=(--this.form.amoun t.value==0);">

</form>
=============== ==========

ie6 tested

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 30 '05 #4
Lee
Evertjan. said:

Lee wrote on 30 sep 2005 in comp.lang.javas cript:
Papajo said:

<form>
<input type="text" name="amount" size="4" value="0">
<input type="button" value="up"
onClick="this.f orm.amount.valu e++;">
<input type="button" value="down"
onClick="if(thi s.form.amount.v alue>'0')this.f orm.amount.valu e--;">
</form>


this.form.amou nt.value>'0'
the parentheses are unnecessary.
Much better to disable the down button when 0 is reached:

============== ==========
<form>

<input type="text" name="amount" size="4" value="2">

<input type="button" value="up"
onClick="this.f orm.amount.valu e++;
document.getEle mentById('down' ).disabled=fals e">

<input type="button" value="down" id='down'
onClick="this.d isabled=(--this.form.amoun t.value==0);">

</form>


How is that much better? It's insignificantly more efficient
in execution, and much more complicated to implement (and for
a new user to understand).

Sep 30 '05 #5
JRS: In article <28************ ****@storefull-3337.bay.webtv. net>,
dated Fri, 30 Sep 2005 14:04:09, seen in news:comp.lang. javascript,
Papajo <do*****@webtv. net> posted :
This simple script counts up or down with a button click, now can it be
modified so the count won't go below zero?
********** Thanks, Joe

<form>
<input type=text name=amount size=4 value=>
<input type=button value="up"
onClick="javas cript:this.form .amount.value++ ;">
<input type=button value="down"
onClick="javas cript:this.form .amount.value--;">
</form>


Consider and adapt

<input type=button value="5"
onClick="javasc ript:this.value -= this.value>0">

Read the newsgroup FAQ for other matters.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 30 '05 #6
Thank you for the responses, I went with Lee's script, it was the
easiest and best for my application. Joe

<form>
<input type="text" name="amount" size="4" value="">

<input type="button" value="up"
onClick="javasc ript:this.form. amount.value++; ">
<input type="button" value="down"
onClick="if(jav ascript:this.fo rm.amount.value >'0')this.form. amount.value--;">
</form>

Oct 1 '05 #7
Dr John Stockton wrote in message news:uF******** ******@merlyn.d emon.co.uk...
Papajo posted:
This simple script counts up or down with a button click, now can it be
modified so the count won't go below zero?
Thanks, Joe

<form>
<input type=text name=amount size=4 value=>
<input type=button value="up"
onClick="javasc ript:this.form. amount.value++; ">
<input type=button value="down"
onClick="javasc ript:this.form. amount.value--;">
</form>


Consider and adapt

<input type=button value="5"
onClick="javasc ript:this.value -= this.value>0">

Read the newsgroup FAQ for other matters.


hmmm...
three questions:
1. why the "javascript :" in onClick="javasc ript:this.value -= this.value>0">
2. if I want to go up, I run into "string" problems:

<input type=text name=amount size=4 value=5>
<input type=button value="down"
onClick="this.f orm.amount.valu e -= this.form.amoun t.value>0">

works wonderfully, but

<input type=button value="up"
onClick="this.f orm.amount.valu e += +this.form.amou nt.value<10">
returns 5true (5truefalse on the next...)
<input type=button value="up"
onClick="this.f orm.amount.valu e += +(this.form.amo unt.value<10)">
returns 51 (and 510 on the second click)

I thought unary+ would type-convert to number, but I notice:
stringVar += +stingVar --> stringVar += numVar

so I end up with
<input type=button value="up"
onClick="this.f orm.amount.valu e = +this.form.amou nt.value + +(this.form.amo unt.value<10)">

isn't there a shortcut or is type-conversion only really good for - * and /?

ok, came up with the unpretty:
<input type=button value="up"
onClick="this.f orm.amount.valu e -= +(this.form.amo unt.value<10) * -1">

this way, the -= type-converts both sides additionally I need to invert the number (- * - = +)
Oct 1 '05 #8
Lee wrote on 01 okt 2005 in comp.lang.javas cript:
Evertjan. said:

Lee wrote on 30 sep 2005 in comp.lang.javas cript:
Papajo said:

<form>
<input type="text" name="amount" size="4" value="0">
<input type="button" value="up"
onClick="this.f orm.amount.valu e++;">
<input type="button" value="down"
onClick="if(thi s.form.amount.v alue>'0')this.f orm.amount.valu e-
-;">
</form>


this.form.amo unt.value>'0'
the parentheses are unnecessary.
Much better to disable the down button when 0 is reached:

============= ===========
<form>

<input type="text" name="amount" size="4" value="2">

<input type="button" value="up"
onClick="this.f orm.amount.valu e++;
document.getEle mentById('down' ).disabled=fals e">

<input type="button" value="down" id='down'
onClick="this.d isabled=(--this.form.amoun t.value==0);">

</form>


How is that much better? It's insignificantly more efficient
in execution, and much more complicated to implement (and for
a new user to understand).


The much better is the computer/human interface showing that the down-
button does not function when the value is zero.

A down button that looks and feels functinal but does not count down is a
bad concept, when disabling is possible.

Proramming is not only about functioning but also about functionality.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 1 '05 #9
To answer your question about the "javascript :" in the onClick is that
this script is being used on a simple webtv browser, it wouldn't work
without it. Thanks again Joe
PS: sorry I didn't mention it in my original post

Oct 1 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3765
by: Tim Hussein | last post by:
Hi, this is my situation: I have a counter on my MAIN page. It is incremented by requesting a certain "picture" which is the output of a script. I can do this manually with: http://www.mydomain.com/script.php?id=123456&vid=987654 This increments the counter.
0
1887
by: deko | last post by:
I use this script to keep count of visits to my site - it's on every page in the site. <?php $file = '/home/post/public_html/viscount'; if ($counter = @file ($file)) { $line = each($counter); if (!$counter) { $counter = 1; } else {
8
2530
by: Jim in Arizona | last post by:
I've been looking for a counting script to count the number of hits to my HTM and asp web pages on my company's internal website. So far, everything I've seen only works on asp pages. Where would I find some counter code to record hits on htm(l) as well as asp pages? I'm using IIS 5.0. Thanks.
2
4477
by: Carl Gilbert | last post by:
Hi I'm having problems with the following code. All I want to do is keep a counter on the page for every new image recieved without deviating from the "for" and "event". <OBJECT ID="CamImage1" WIDTH=352 HEIGHT=288 CLASSID="CLSID:C79D3167-6133-4e7c-821C-5C114611022D" CODEBASE="http://www.ipvideocenter.com/components/CameraControl.cab#VERSION= 3,2,1,0">
7
3417
by: mistral | last post by:
I use htaccess to protect directory and granting access to download file only for the authorized users. Just want implement simple PHP file download counter for single file. I need track the number of downloads of this file on my website, IP address and date. Since I have no access to Apache log files, I need some other way, use script that will write a log file. Is there some handy way? M.
5
2087
by: jasonchan | last post by:
How would you set up a counter in javascript that goes from 5 to 0 This is what i have so far and it is not displaying in the div container "counter" for some reason... <!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 http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled...
3
7877
by: Abhinavnaresh | last post by:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Xml.Xsl.XsltException: Cannot find the script or external object that implements prefix 'counter'. Source Error: An unhandled exception was generated during the execution of the current web request. Information...
9
2245
by: Pygmalion | last post by:
I have found dozen of useful PHP counters on the web. However, nobody is working for my web pages, since administrator does not want to enable the possibility that PHP could be called from HTML. (i.e. "Addhandler application/x-httpd-php .html .php" in .htaccess). He says it is a security problem. Is it possible to make a PHP counter WITHOUT using echo command? Thanks for your suggestions,
2
2851
by: gumbercules | last post by:
I am designing a site that is regulary updated with new podcasts. I would like to be able to have a counter next to each podcast showing how many hits/listens/plays/views each particular podcast has gotten. eg: podcast001.mp3 plays: 12 I have set up a column in a mysql database for each podcast called 'counter'. I then tried to make a php-javascript hybrid code with functions on onClick that went into and added one to the counter...
3
3087
by: DavidPr | last post by:
I made a website for a group of people and they want a visitor counter on it. I don't like hit counters because if the site is dead everyone will know it. But, it's their website. I did an Internet search but didn't find exactly what I was looking for. I did find an old tutorial, from which I've pasted the code below. I need it to be session controlled and I only want it to count unique visitors. I want to use a database to keep track of...
0
8238
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7164
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1485
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.