Quick Question - Javascript Newbie

Discussion in 'Javascript' started by Jon, Jun 14, 2004.

  1. Jon

    Jon Guest

    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
     
    Jon, Jun 14, 2004
    #1
    1. Advertising

  2. Jon

    Grant Wagner Guest

    Jon wrote:

    > 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 <>

    * 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
     
    Grant Wagner, Jun 14, 2004
    #2
    1. Advertising

  3. Jon schrieb:
    > 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
     
    Thomas 'PointedEars' Lahn, Jun 15, 2004
    #3
  4. Grant Wagner schrieb:
    > Jon 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>
    >> [...]

    > "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
     
    Thomas 'PointedEars' Lahn, Jun 15, 2004
    #4
  5. JRS: In article <>, seen in
    news:comp.lang.javascript, Thomas 'PointedEars' Lahn
    <> posted at Tue, 15 Jun 2004 18:50:18 :
    >
    >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.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
     
    Dr John Stockton, Jun 15, 2004
    #5
  6. Dr John Stockton <> writes:

    > JRS: In article <>, seen in
    > news:comp.lang.javascript, Thomas 'PointedEars' Lahn
    > <> posted at Tue, 15 Jun 2004 18:50:18 :
    >>
    >>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.


    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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jun 16, 2004
    #6
  7. JRS: In article <>, seen in
    news:comp.lang.javascript, Lasse Reichstein Nielsen <>
    posted at Wed, 16 Jun 2004 22:11:48 :
    >Dr John Stockton <> writes:
    >> JRS: In article <>, seen in
    >> news:comp.lang.javascript, Thomas 'PointedEars' Lahn
    >> <> posted at Tue, 15 Jun 2004 18:50:18 :
    >>>
    >>>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.

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

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
     
    Dr John Stockton, Jun 17, 2004
    #7
  8. Jon

    Grant Wagner Guest

    Thomas 'PointedEars' Lahn wrote:

    >
    > > "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.


    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 <>

    * 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
     
    Grant Wagner, Jun 21, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wizard
    Replies:
    2
    Views:
    371
    Saravana
    Jan 3, 2005
  2. =?Utf-8?B?UnVkeQ==?=

    Quick Newbie Question

    =?Utf-8?B?UnVkeQ==?=, May 17, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    554
    Scott M.
    May 18, 2005
  3. Starwynd
    Replies:
    1
    Views:
    410
    Tor Iver Wilhelmsen
    Nov 23, 2003
  4. JKop
    Replies:
    11
    Views:
    935
  5. Dilip
    Replies:
    5
    Views:
    329
    Fei Liu
    Apr 21, 2006
Loading...

Share This Page