473,758 Members | 4,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript">
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="javasc ript:increment( temporary);">+</a>&nbsp;
<a href="#" onClick="javasc ript: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 13294
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="JavaS cript">
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.te mporary;

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="javasc ript: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:p ointer;"
onclick="modVal ue(document.tmp .temporary, 1);">+</span>&nbsp;

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


<span style="cursor:p ointer; "
onclick="modVal ue(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="JavaS cript">


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.ne t.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="JavaS cript">


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.te mporary;

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="javasc ript: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:p ointer;"
onclick="modVal ue(document.tmp .temporary, 1);">+</span>&nbsp;

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


<span style="cursor:p ointer; "
onclick="modVal ue(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$160 36$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.ne t.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.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.
May 10 '06 #6

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

Similar topics

9
3191
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 post-increment or post-decrement operator modifies the operand once the entire statement has been executed, not during execution of the statement, so this confused me. An examples given to illustrate the ambiguity is: a = i++; // may increment...
8
6268
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 // // >g++ test.cpp // test.cpp: In function `int main()':
2
12717
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 telling me that "Number of query values and destination fields are not the same." If I add a value for the auto increment field to the SQL String the data is entered into the table with no problems but obviously the auto increment field now...
3
2744
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 manner. But at the same time if i want to use variable that could be changed in another thread i must use volatile (to prevent optimization). But then i can not use Interlocked.Increment. So i do not see any benefits of having ...
2
1509
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 I'm trying to change the value of a text box based on if the up or down arrow was pressed. (Spinner box). I am getting an error and I'm not quite sure how to go about debugging.
2
3156
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 then the calender gone actually i want if i click outside off the calender then it should me removed ..How kan i do this ... Pls inform me as early as possible .. I am waiting for ur quick replay ...Here i attached the source code .... <!DOCTYPE...
6
2438
by: Kevin Walzer | last post by:
This code: #include <stdio.h> int main(void) { int n1, n2; //two integers n1 = 1; n2 = 1;
13
9836
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 handled with the same overload, but I just want to make sure.
1
2586
by: KRISHNA PRAVI | last post by:
the error is "runtime error object expected" here is the code....................................................................................... <script language="javascript" src="../Scripts/calender.js"></script> </HEAD> <body bgColor="white" MS_POSITIONING="GridLayout"> <form id="Form1" style="LEFT: 0px; POSITION: absolute; TOP: 0px" method="post" runat="server"> <!--<table width="100%" style="LEFT: 0px; POSITION:...
0
9489
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
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
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
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
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.