Working on forms where element names have spaces

Discussion in 'Javascript' started by PeteC, Jul 12, 2005.

  1. PeteC

    PeteC Guest

    I'm working on a web page which has some form elements in it which I need to
    manipulate using JavaScript.

    If the element is (for example, in a form called TicketForm)

    <INPUT name="intNumTickets" onBlur="calcPrice();" value="0">

    I can read and manipulate this value in CalcPrice using the format

    document.TicketForm.intNumTickets

    My problem is, this form needs to pass the values to a payment processor,
    and they say that the name needs to be preceded with the text 'PT' so that
    it is passed back to the form later without any processing (no, I cannot
    change this aspect).

    So the input statement would be

    <INPUT name="PT intNumTickets" onBlur="calcPrice();" value="0">

    ....but my problem is, how do I reference PT intNumTickets using the document
    format?

    document.TicketForm.PT intNumTickets

    won't work.

    Any ideas how I can handle this?

    Regards,


    Pete.
    PeteC, Jul 12, 2005
    #1
    1. Advertising

  2. PeteC wrote:

    > <INPUT name="PT intNumTickets" onBlur="calcPrice();" value="0">
    > ...but my problem is, how do I reference PT intNumTickets using the
    > document format?
    > document.TicketForm.PT intNumTickets
    > won't work.


    > Any ideas how I can handle this?


    http://jibbering.com/faq/#FAQ4_25

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
    David Dorward, Jul 12, 2005
    #2
    1. Advertising

  3. PeteC wrote:

    > <INPUT name="PT intNumTickets" onBlur="calcPrice();" value="0">
    >
    > ...but my problem is, how do I reference PT intNumTickets using the document
    > format?


    You use the square brackets notation, i.e.
    document.forms["myForm"].elements["PT intNumTickets"];

    See the FAQ, especially
    <URL:http://jibbering.com/faq/#FAQ4_39>


    Regards,
    Yep.
    Yann-Erwan Perio, Jul 12, 2005
    #3
  4. PeteC

    web.dev Guest

    Hi Pete,

    >>they say that the name needs to be preceded with the text 'PT'...

    [snip]
    >>.. name="PT intNumTickets" ...


    First of all, they said that it needed to preceded with 'PT', but did
    they also say it had to have a space in between? If that is still the
    case, then yes there is another way for you to reference the form
    elements. Try using the following way:

    document.forms[x].elements[y]

    For example, let's say you have only 1 form:

    <form name = "foo" action = "url" method = "post" onsubmit = "return
    myFunc();">
    <input name = "PT intAbc"/>
    <input name = "PT intNumTickets"/>
    <input name = "PT intXyz"/>
    </form>

    You can then go through your elements by the following:

    for(var i = 0; i < 3; i++)
    {
    var tmp = document.forms[0].elements.value;
    //code to do something with the value
    }

    Where 3 (if you know beforehand) is the number of elements in the form.

    Hope this helps. :)
    web.dev, Jul 12, 2005
    #4
  5. PeteC

    PeteC Guest

    web.dev wrote:
    > Hope this helps. :)


    Certainly does. Many thanks.

    Pete.
    PeteC, Jul 12, 2005
    #5
  6. PeteC

    Zif Guest

    Danny wrote:
    >
    > Spaces are not allowed in ID/Names per standards:
    >
    > HTML4.1 and CSS definition on CDATA which is what name/attributes use
    >
    > ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
    > followed by any number of letters, digits ([0-9]), hyphens ("-"),
    > underscores ("_"), colons (":"), and periods (".").
    >


    That is *not* the specification for CDATA, it is for SGML ID and NAME
    tokens (that's why it says ID and NAME tokens in the text).

    The CDATA spec is:

    " CDATA is a sequence of characters from the document character set and
    may include character entities. User agents should interpret attribute
    values as follows:

    * Replace character entities with characters,
    * Ignore line feeds,
    * Replace each carriage return or tab with a single space.

    "User agents may ignore leading and trailing white space in CDATA
    attribute values (e.g., " myval " may be interpreted as "myval").
    Authors should not declare attribute values with leading or trailing
    white space.

    "For some HTML 4 attributes with CDATA attribute values, the
    specification imposes further constraints on the set of legal values
    for the attribute that may not be expressed by the DTD.

    "Although the STYLE and SCRIPT elements use CDATA for their data model,
    for these elements, CDATA must be handled differently by user agents.
    Markup and entities must be treated as raw text and passed to the
    application as is. The first occurrence of the character sequence "</"
    (end-tag open delimiter) is treated as terminating the end of the
    element's content. In valid documents, this would be the end tag for
    the element."

    None of which disallows the use of spaces.


    --
    Zif
    Zif, Jul 13, 2005
    #6
  7. Danny wrote:

    > ID and NAME tokens


    Unintuitive though it may be, form control name attributes don't take NAME
    tokens for their values. Spaces are OK.

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
    David Dorward, Jul 13, 2005
    #7
    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. Carl
    Replies:
    0
    Views:
    521
  2. Ragnar Heil
    Replies:
    4
    Views:
    580
    Ragnar Heil
    Oct 25, 2004
  3. Replies:
    2
    Views:
    501
  4. John B. Matthews
    Replies:
    4
    Views:
    666
    John B. Matthews
    Sep 12, 2008
  5. Roedy Green
    Replies:
    3
    Views:
    620
Loading...

Share This Page