473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

JavaScript Increment/Decrement Form Field Value

Hi all,

Iv'e got a page that has a mass amount of input fields, all of which require
a decimal figure. To make it easier when it comes to inputting data, I'm
trying to setup + and - links that will increment/decrement the form field
value by 1 when clicked.

I'm using the following code, however when I click on one of the links, I
get the following error -

document.forms.tmp.input_field is null or not an object.

It's as if JavaScript is not reading the parameter being passed through the
function.

Here's the code -

<html>
<head>
<script language="JavaScript">
function increment(input_field) {
document.forms.tmp.input_field.value = document.forms.tmp.input_field.value
+ 1;
document.forms.tmp.input_field.focus();
}
function decrement(input_field) {
document.forms.tmp.input_field.value =
document.forms.tmp.input_field.value - 1;
document.forms.tmp.input_field.focus();
}
</script>
</head>
<body>
<form name="tmp">
<p><input type="text" name="temporary" id="temporary" value="0">&nbsp;
<a href="#" onClick="javascript:increment(temporary);">+</a>&nbsp;
<a href="#" onClick="javascript:decrement(temporary);">-</a></p>
</form>
</body>
</html>

Any help would be appreciated. I just can't seem to see the problem...

Thanks in advance,

Stuart
May 10 '06 #1
5 13259
Stuart wrote:
<snip>
I'm using the following code, however when I click on one of the links, I
get the following error -

document.forms.tmp.input_field is null or not an object.

It's as if JavaScript is not reading the parameter being passed through
the function. <snip> function increment(input_field) {
document.forms.tmp.input_field.value =
document.forms.tmp.input_field.value + 1;
document.forms.tmp.input_field.focus();
}


Your - input_field - parameter is irrelevant to the evaluation of the
dot-notation property accessor - document.forms.tmp.input_field.value
-, in that context the 'input_field' is expected to correspond
literally with a property name of the form object.

You should use bracket notation property accessors if you want property
names to be dynamically determined from an expression:-

<URL: http://jibbering.com/faq/#FAQ4_39 >

Richard.

May 10 '06 #2
Stuart wrote:
Hi all,

Iv'e got a page that has a mass amount of input fields, all of which require
a decimal figure. To make it easier when it comes to inputting data, I'm
trying to setup + and - links that will increment/decrement the form field
value by 1 when clicked.

I'm using the following code, however when I click on one of the links, I
get the following error -

document.forms.tmp.input_field is null or not an object.

It's as if JavaScript is not reading the parameter being passed through the
function.

Here's the code -

<html>
<head>
<script language="JavaScript">
The language attribute is deprecated, type is required:

<script type="text/javascript">

function increment(input_field) {
document.forms.tmp.input_field.value = document.forms.tmp.input_field.value
+ 1;
Firstly, the name/if of the field is "temporary", there is no field
called 'input_field'.

Secondly, you might consider standardising how you access form controls.

Thirdly, the value of the field is always a string, so you need to
convert it to a number in order to perform addition. The simplest way
to do that is to use the unary '+' operator. Putting it all together,
you get:

var inp = document.forms['tmp'].elements['temporary'];
inp.value = +inp.value + 1;
A shorter alternative for the form control reference is:

var inp = document.tmp.temporary;

document.forms.tmp.input_field.focus();
}
function decrement(input_field) {
document.forms.tmp.input_field.value =
document.forms.tmp.input_field.value - 1;
document.forms.tmp.input_field.focus();
}
You could write a single function that does both:

function modValue(el, n){
el.value = +el.value + n;
if (el.focus) el.focus();
}

of course you need to ensure that the value is a number before
attempting anything.

</script>
</head>
<body>
<form name="tmp">
<p><input type="text" name="temporary" id="temporary" value="0">&nbsp;
<a href="#" onClick="javascript:increment(temporary);">+</a>&nbsp;
There is no need to use an A element, nor to use 'javascript:' in the
onclick attribute. It is non-standard to reference an element by its
name/ID, pass a reference to the form control using correct syntax:

<span style="cursor:pointer;"
onclick="modValue(document.tmp.temporary, 1);">+</span>&nbsp;

<a href="#" onClick="javascript:decrement(temporary);">-</a></p>


<span style="cursor:pointer; "
onclick="modValue(document.tmp.temporary, -1);">-</span></p>
Of course you should probably use a class rather than in-line CSS.

[...]
--
Rob
May 10 '06 #3
RobG wrote:
Stuart wrote:
Hi all,

Iv'e got a page that has a mass amount of input fields, all of which
require a decimal figure. To make it easier when it comes to inputting
data, I'm trying to setup + and - links that will increment/decrement
the form field value by 1 when clicked.

I'm using the following code, however when I click on one of the
links, I get the following error -

document.forms.tmp.input_field is null or not an object.

It's as if JavaScript is not reading the parameter being passed
through the function.

Here's the code -

<html>
<head>
<script language="JavaScript">


The language attribute is deprecated, type is required:

<script type="text/javascript">

function increment(input_field) {
document.forms.tmp.input_field.value =
document.forms.tmp.input_field.value + 1;


Firstly, the name/if of the field is "temporary", there is no field
called 'input_field'.


Sorry, missed the use of input_field. You could use it in referencing
the form as:

document.forms.tmp.elements[input_field].value

but I think passing a reference rather than the name/id is better anyway.

[...]
--
Rob
May 10 '06 #4

"RobG" <rg***@iinet.net.au> wrote in message
news:44***********************@per-qv1-newsreader-01.iinet.net.au...
Stuart wrote:
Hi all,

Iv'e got a page that has a mass amount of input fields, all of which
require a decimal figure. To make it easier when it comes to inputting
data, I'm trying to setup + and - links that will increment/decrement the
form field value by 1 when clicked.

I'm using the following code, however when I click on one of the links, I
get the following error -

document.forms.tmp.input_field is null or not an object.

It's as if JavaScript is not reading the parameter being passed through
the function.

Here's the code -

<html>
<head>
<script language="JavaScript">


The language attribute is deprecated, type is required:

<script type="text/javascript">

function increment(input_field) {
document.forms.tmp.input_field.value =
document.forms.tmp.input_field.value + 1;


Firstly, the name/if of the field is "temporary", there is no field called
'input_field'.

Secondly, you might consider standardising how you access form controls.

Thirdly, the value of the field is always a string, so you need to convert
it to a number in order to perform addition. The simplest way to do that
is to use the unary '+' operator. Putting it all together, you get:

var inp = document.forms['tmp'].elements['temporary'];
inp.value = +inp.value + 1;
A shorter alternative for the form control reference is:

var inp = document.tmp.temporary;

document.forms.tmp.input_field.focus();
}
function decrement(input_field) {
document.forms.tmp.input_field.value =
document.forms.tmp.input_field.value - 1;
document.forms.tmp.input_field.focus();
}


You could write a single function that does both:

function modValue(el, n){
el.value = +el.value + n;
if (el.focus) el.focus();
}

of course you need to ensure that the value is a number before attempting
anything.

</script>
</head>
<body>
<form name="tmp">
<p><input type="text" name="temporary" id="temporary" value="0">&nbsp;
<a href="#" onClick="javascript:increment(temporary);">+</a>&nbsp;


There is no need to use an A element, nor to use 'javascript:' in the
onclick attribute. It is non-standard to reference an element by its
name/ID, pass a reference to the form control using correct syntax:

<span style="cursor:pointer;"
onclick="modValue(document.tmp.temporary, 1);">+</span>&nbsp;

<a href="#" onClick="javascript:decrement(temporary);">-</a></p>


<span style="cursor:pointer; "
onclick="modValue(document.tmp.temporary, -1);">-</span></p>
Of course you should probably use a class rather than in-line CSS.

[...]
--
Rob


Your modValue() function was just what I needed. Thanks for that. Also, the
problem with the adding/subtracting was going to be my next question...

Thanks for your help,

Stuart
May 10 '06 #5
JRS: In article <4461e3c8$0$16036$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Wed, 10 May 2006 22:59:49 remote, seen in
news:comp.lang.javascript, RobG <rg***@iinet.net.au> posted :
Thirdly, the value of the field is always a string, so you need to
convert it to a number in order to perform addition.
That's virtually a terminological inexactitude, as it appears to happen
- see below. Change "you need to convert it" to "it must be converted"
and all is (in either case) well with the sentence, except for possibly
being superfluous.
The simplest way
to do that is to use the unary '+' operator.
Agreed.
Putting it all together,
you get:

var inp = document.forms['tmp'].elements['temporary'];
inp.value = +inp.value + 1;

In my js-quick.htm, using IE4, executing

document.forms['F'].elements['X0'].value++

increments that field numerically. Decrements likewise.

The remaining question is whether that code line is sanctioned by
applicable standards and/or tests in sufficient browsers.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
May 10 '06 #6

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

Similar topics

9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
2
by: Tom | last post by:
I am trying to store information into a table that has an auto increment field. There is currently no data in the table. Using the code below I cannot insert data into the table. I get an error...
3
by: George Ter-Saakov | last post by:
What is the purpose of having Interlocked.Increment if it does not work with variable declared as volatile. Here is my problem, Interlocked.Increment increments the variable in thread safe...
2
by: Guy Noir | last post by:
Hello all. I am new to Javascript but versed in JAva, c#, etc. I'm running into a problem and I'm hoping someone can point me in the right direction. I have 2 images (Up and down arrows) and...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
6
by: Kevin Walzer | last post by:
This code: #include <stdio.h> int main(void) { int n1, n2; //two integers n1 = 1; n2 = 1;
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
1
by: KRISHNA PRAVI | last post by:
the error is "runtime error object expected" here is the code....................................................................................... <script language="javascript"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.