Connecting Tech Pros Worldwide Help | Site Map

JavaScript Increment/Decrement Form Field Value

Stuart
Guest
 
Posts: n/a
#1: May 10 '06
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


Richard Cornford
Guest
 
Posts: n/a
#2: May 10 '06

re: JavaScript Increment/Decrement Form Field Value


Stuart wrote:
<snip>[color=blue]
> 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.[/color]
<snip>[color=blue]
> function increment(input_field) {
> document.forms.tmp.input_field.value =
> document.forms.tmp.input_field.value + 1;
> document.forms.tmp.input_field.focus();
> }[/color]

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.

RobG
Guest
 
Posts: n/a
#3: May 10 '06

re: JavaScript Increment/Decrement Form Field Value


Stuart wrote:[color=blue]
> 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">[/color]

The language attribute is deprecated, type is required:

<script type="text/javascript">

[color=blue]
> function increment(input_field) {
> document.forms.tmp.input_field.value = document.forms.tmp.input_field.value
> + 1;[/color]

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;

[color=blue]
> 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();
> }[/color]

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.

[color=blue]
> </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;[/color]

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;

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

<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
RobG
Guest
 
Posts: n/a
#4: May 10 '06

re: JavaScript Increment/Decrement Form Field Value


RobG wrote:[color=blue]
> Stuart wrote:[color=green]
>> 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">[/color]
>
> The language attribute is deprecated, type is required:
>
> <script type="text/javascript">
>
>[color=green]
>> function increment(input_field) {
>> document.forms.tmp.input_field.value =
>> document.forms.tmp.input_field.value + 1;[/color]
>
> Firstly, the name/if of the field is "temporary", there is no field
> called 'input_field'.[/color]

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
Stuart
Guest
 
Posts: n/a
#5: May 10 '06

re: JavaScript Increment/Decrement Form Field Value



"RobG" <rgqld@iinet.net.au> wrote in message
news:4461e3c8$0$16036$5a62ac22@per-qv1-newsreader-01.iinet.net.au...[color=blue]
> Stuart wrote:[color=green]
>> 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">[/color]
>
> The language attribute is deprecated, type is required:
>
> <script type="text/javascript">
>
>[color=green]
>> function increment(input_field) {
>> document.forms.tmp.input_field.value =
>> document.forms.tmp.input_field.value + 1;[/color]
>
> 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;
>
>[color=green]
>> 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();
>> }[/color]
>
> 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.
>
>[color=green]
>> </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;[/color]
>
> 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;
>
>[color=green]
>> <a href="#" onClick="javascript:decrement(temporary);">-</a></p>[/color]
>
> <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[/color]

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


Dr John Stockton
Guest
 
Posts: n/a
#6: May 10 '06

re: JavaScript Increment/Decrement Form Field Value


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 <rgqld@iinet.net.au> posted :
[color=blue]
>Thirdly, the value of the field is always a string, so you need to
>convert it to a number in order to perform addition.[/color]

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.
[color=blue]
> The simplest way
>to do that is to use the unary '+' operator.[/color]

Agreed.
[color=blue]
> Putting it all together,
>you get:
>
> var inp = document.forms['tmp'].elements['temporary'];
> inp.value = +inp.value + 1;[/color]


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.
Closed Thread


Similar JavaScript / Ajax / DHTML bytes