using 'name' or using 'id' of a form?

Discussion in 'Javascript' started by Carl, Sep 5, 2003.

  1. Carl

    Carl Guest

    When using 'name' in the form, it works, when using 'id' it doesn't. Any
    comments about this? By the way, is this a good method or is it better to
    use 'getElementById'?
    Carl

    <body>
    <form name="myform">
    <input type button id="antw1" value="test">
    </form>

    <script type="text/javascript">
    res=document.myform.antw1.value
    alert(res)
    </script>
     
    Carl, Sep 5, 2003
    #1
    1. Advertising

  2. "Carl" wrote in message news:bja9er$5a6$...
    > When using 'name' in the form, it works, when using 'id' it doesn't.

    Any
    > comments about this? By the way, is this a good method or is it better

    to
    > use 'getElementById'?
    > Carl
    >
    > <body>
    > <form name="myform">
    > <input type button id="antw1" value="test">
    > </form>
    >
    > <script type="text/javascript">
    > res=document.myform.antw1.value
    > alert(res)
    > </script>
    >


    I usually give my forms both a name and an ID (both the same).
    Then i reference them by document.forms['myForm']
    i.e. document.forms['myform'].antw1.value

    getElementById() is the most recent method and is only available in IE5+
    and NN6+.
    It might be a good idea to get used to that method now, but make sure
    you include and option for older browsers.
    I.e.
    d=document // to ease further scripting
    if (myObject=d.getElementById('myform')) {
    res=myObject.antw1.value
    } else {
    res=d.forms['myforms'].antw1.value
    }

    If you don't need the form tags you can access the objects directly
    though;
    res=antw1.value

    Hope this helped.


    --
    B Alvestad
    aka BraveBrain
     
    Børge Alvestad, Sep 5, 2003
    #2
    1. Advertising

  3. Carl

    Grant Wagner Guest

    "Børge Alvestad" wrote:

    > "Carl" wrote in message news:bja9er$5a6$...
    > > When using 'name' in the form, it works, when using 'id' it doesn't.

    > Any
    > > comments about this? By the way, is this a good method or is it better

    > to
    > > use 'getElementById'?
    > > Carl
    > >
    > > <body>
    > > <form name="myform">
    > > <input type button id="antw1" value="test">
    > > </form>
    > >
    > > <script type="text/javascript">
    > > res=document.myform.antw1.value
    > > alert(res)
    > > </script>
    > >

    >
    > I usually give my forms both a name and an ID (both the same).
    > Then i reference them by document.forms['myForm']
    > i.e. document.forms['myform'].antw1.value
    >
    > getElementById() is the most recent method and is only available in IE5+
    > and NN6+.
    > It might be a good idea to get used to that method now, but make sure
    > you include and option for older browsers.
    > I.e.
    > d=document // to ease further scripting
    > if (myObject=d.getElementById('myform')) {
    > res=myObject.antw1.value
    > } else {
    > res=d.forms['myforms'].antw1.value
    > }
    >
    > If you don't need the form tags you can access the objects directly
    > though;
    > res=antw1.value
    >
    > Hope this helped.
    >
    > --
    > B Alvestad
    > aka BraveBrain


    document.forms['formName'].elements['elementName'] (or
    document.forms['formName'].elementName) is still part of the standard.
    There's no reason to test for and use getElementById(). If getElementById()
    /is/ present, then you know you can still retrieve the value of a form
    element using document.forms[...].elements[...].

    As for giving your form elements the same NAME and ID. While it seems
    economical and seems to work reliably across the majority of browsers,
    recent discussions in this newsgroup have convinced me it's a bad practice.
    This is especially true of things like checkboxes and radio buttons, which
    can have several input instances with the same NAME, but each one should
    have a unique ID.

    As a result, I've begun using something like NAME="myInputNAME"
    ID="myInputID" (I haven't quite decided on the best naming scheme yet). For
    groups of radio buttons, it's NAME="myRadioNAME" ID="myRadioID1" (or maybe
    ID="myRadio1ID", as I said, I haven't arrived at a conclusion as to what's
    better).

    Anyway, the point was, there's no reason to test for and use
    getElementById(). If it's supported, then so is
    document.forms[...].elements[...].

    --
    | 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, Sep 5, 2003
    #3
  4. > When using 'name' in the form, it works, when using 'id' it doesn't. Any
    > comments about this? By the way, is this a good method or is it better to
    > use 'getElementById'?


    'id' is used to access specific objects in the DOM. 'name', in form elements, is
    used to fill in the names in the postdata that is generated when the form is
    submitted. Early browsers tended to use one as the default for the other, which
    led to general confusion about the roles of the attributes.

    http://www.crockford.com/#javascript
     
    Douglas Crockford, Sep 5, 2003
    #4
  5. Reply to "Grant Wagner", message
    news:...

    Thanks for useful information, Grant ;)


    --
    Børge Alvestad
    aka BraveBrain
     
    Børge Alvestad, Sep 6, 2003
    #5
    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. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    863
    Roedy Green
    Jan 2, 2006
  2. ding feng
    Replies:
    2
    Views:
    2,858
    ding feng
    Jun 25, 2003
  3. Java  script  Dude

    IE name="name" & form.name property bug

    Java script Dude, Jun 29, 2004, in forum: Javascript
    Replies:
    5
    Views:
    261
    Java script Dude
    Jun 30, 2004
  4. Pavils Jurjans
    Replies:
    3
    Views:
    208
    Pavils Jurjans
    Mar 18, 2005
  5. Replies:
    6
    Views:
    191
Loading...

Share This Page