Quick Question - Javascript Newbie

J

Jon

Hi all,

I'm doing a small project for work and I have this problem, I have a
text area, named with a unique identifier (the name, although I have
tried to put in an ID as well). I have a number in this text field,
and I want to have a button to the right of it that increments this
number by one. The problem is that so far I am unable to figure out
how to find th element using javascript. I pass the identifier into a
javascript function I wrote:

function increment(ident)
{
var level = document.getElementById(ident);
level_val = parseInt(level.value,10);
level_val++;
level.value = level_val;
}

The HTML that holds the input area is here...

<td align=center><input type=text value="Not Priced" name=25881 size=7
maxlength=10 ></td>
<td><INPUT type="button" name="increment25881" value="+1"
onClick=increment("25881")></td>

And it's not incrementing. The form is called tableprices. Another
thing I did, that I thought should work, is type in a value, 87, into
the text input field and then tried incrementing, but it still didn't
work. Any ideas? Thanks!

Jon
 
G

Grant Wagner

Jon said:
Hi all,

I'm doing a small project for work and I have this problem, I have a
text area, named with a unique identifier (the name, although I have
tried to put in an ID as well). I have a number in this text field,
and I want to have a button to the right of it that increments this
number by one. The problem is that so far I am unable to figure out
how to find th element using javascript. I pass the identifier into a
javascript function I wrote:

function increment(ident)
{
var level = document.getElementById(ident);
level_val = parseInt(level.value,10);
level_val++;
level.value = level_val;
}

The HTML that holds the input area is here...

<td align=center><input type=text value="Not Priced" name=25881 size=7
maxlength=10 ></td>
<td><INPUT type="button" name="increment25881" value="+1"
onClick=increment("25881")></td>

And it's not incrementing. The form is called tableprices. Another
thing I did, that I thought should work, is type in a value, 87, into
the text input field and then tried incrementing, but it still didn't
work. Any ideas? Thanks!

Jon

"name" attributes should not start with a number, also, you're using
getElementById, but it's the "name" attribute you're looking for (IE
allows getElementById() to retrieve elements whose "name" matches the
string, Mozilla does not).

And how do you propose to add one to "Not Priced"?

Anyway:

<script type="text/javascript">
function increment(id) {
var level = document.getElementById("id" + id);
var level_val = +level.value;
if (isNaN(level_val)) {
level_val = 0;
}
level.value = level_val + 1;
}
</script>
<input type="text" value="Not Priced" name="name25881" id="id25881"
size="7" maxlength="10">
<input type="button" name="increment25881" value="+1"
onclick="increment(this);">

Better yet is to wrap the entire page in <form></form>, then you could
use:

<script type="text/javascript">
function increment(btn) {
var name = btn.name.substring(9);
var level_val = +btn.form.elements['name' + name].value;
if (isNaN(level_val)) {
level_val = 0;
}
btn.form.elements['name' + name].value = level_val + 1;
}
</script>
<form>
<input type="text" value="Not Priced" name="name25881" size="7"
maxlength="10">
<input type="button" name="increment25881" value="+1"
onclick="increment(this);">
</form>

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
T

Thomas 'PointedEars' Lahn

Jon said:
function increment(ident)
{
var level = document.getElementById(ident);

You are trying to access the element by its ID, although it has only
a "name" attribute value.
And it's not incrementing. The form is called tableprices. [...]

Access the form element with

<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
...
</head>

<body>
...
[...]<input type="button" value="+1"
onclick="+form.elements['25881'].value++">[...]
...
</body>

(A "name" attribute value is not required for the button.)

The unary "+" operator may not be available, you can also use

J(ava)Script/ECMAScript:

function increment(o)
{
var i = parseInt(o.value, 10);
if (!isNaN(i))
{
o.value = ++i;
}
}

HTML:

[...]<input type="button" value="+1"
onclick="increment(form.elements['25881'])">[...]

Note that "form" is a property of the object referenced by "this" but
the use of the scope chain makes this additional lookup unnecessary.

Besides, you should watch more for Pretty Printing, especially
indentation. It makes your code easier legible and thus understandable.

And your Subject header should have been much more descriptive.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Grant said:
Jon said:
function increment(ident)
{
var level = document.getElementById(ident);
level_val = parseInt(level.value,10);
level_val++;
level.value = level_val;
}

The HTML that holds the input area is here...

<td align=center><input type=text value="Not Priced" name=25881 size=7
maxlength=10 ></td>
<td><INPUT type="button" name="increment25881" value="+1"
onClick=increment("25881")></td>
[...]
"name" attributes should not start with a number,

Why not? Only the value of the "name" attribute of the META element is
of type META, the values of other "name" attributes are of type CDATA.
They may start with *any* character.

RFCs state that a signature must be delimited with a line containing
only "-- " (dashDashSpace) and recommend that it should not exceed 4
lines and 80 characters per line. The FAQ of this newsgroup recommends
that quotes should be trimmed to the absolute necessary.


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Thomas 'PointedEars' Lahn
RFCs state that a signature must be delimited with a line containing
only "-- " (dashDashSpace) and recommend that it should not exceed 4
lines and 80 characters per line.

Please give the RFC numbers and if possible paragraph numbers.
 
L

Lasse Reichstein Nielsen

Dr John Stockton said:
JRS: In article <[email protected]>, seen in
Thomas 'PointedEars' Lahn


Please give the RFC numbers and if possible paragraph numbers.

I believe the only RFC that mentions the "DASH DASH SP" separator is
RFC 2646 section 4.3 and 5. However, it only mentions it as a
convention in Usenet messages, allowing for it to be an exception to
the normal rules for reflowing format=flowed text messages. It is
not an RFC *should* recommendation.

As for the size, I have been corrected before in this group, I believe.
The four lines can be traced back to RFC 1855 (Netiquette Guideline)
section 2.1.1:
---
- If you include a signature keep it short. Rule of thumb
is no longer than 4 lines.
---
Same RFC also says:
---
- Limit line length to fewer than 65 characters and end a line
with a carriage return.
---

Generally, RFC 1855 should be mandatory reading :) It even advocates
normal usenet quoting (as opposed to top posting):
---
- If you are sending a reply to a message or a posting be sure you
summarize the original at the top of the message, or include just
enough text of the original to give a context. This will make sure
readers understand when they start to read your response. Since
NetNews, especially, is proliferated by distributing the postings
from one host to another, it is possible to see a response to a
message before seeing the original. Giving context helps everyone.
But do not include the entire original!
---

All this will, hopefully, some day become an RFC when UseFor completes:
<URL:http://www.landfield.com/usefor/>

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
I believe the only RFC that mentions the "DASH DASH SP" separator is
RFC 2646 section 4.3 and 5.
...
As for the size, I have been corrected before in this group, I believe.
The four lines can be traced back to RFC 1855 (Netiquette Guideline)
section 2.1.1:

Son-of-RFC1036 may be earlier :
it should be at ftp://ftp.demon.co.uk/pub/news/doc/so1036.txt but I
expect it is also on the Web.

From
<http://www.landfield.com/faqs/usenet/software/good-netkeeping-seal/>
GNKSA 2.0 :- "A widely accepted standard is the so-called McQuary limit:
up to 4 lines, each up to a maximum of 80 characters."

I prefer fewer than 80 (i.e. 79) - at least one Oxford user had a
terminal that wrapped after 80 characters.

Generally, RFC 1855

( A.k.a. FYI28. )


Thanks for the effort. But my post was a public request to the Vulcan,
not a request for information.

Web <URL:http://www.merlyn.demon.co.uk/news-use.htm#Sig> contains most
of what I know on the subject.
 
G

Grant Wagner

Thomas said:
Why not? Only the value of the "name" attribute of the META element is
of type META, the values of other "name" attributes are of type CDATA.
They may start with *any* character.

Because it will make it impossible to use dot notation to reference the
element and it's incredibly confusing viewing the source code and if you
choose to submit the data using GET. Just because something can be done does
not mean it should be done, or that doing it is best practice.
RFCs state that a signature must be delimited with a line containing
only "-- " (dashDashSpace) and recommend that it should not exceed 4
lines and 80 characters per line. The FAQ of this newsgroup recommends
that quotes should be trimmed to the absolute necessary.

Is the recommendation that quotes be trimmed the reason you quoted 19 lines
when all you responded to was the last line consisting of [..."name"
attributes should not start with a number,...]?

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top