What's the difference between name and id?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Man-wai Chang

    #1

    What's the difference between name and id?


    Why 2 reference points?

    --
    iTech Consulting Co., Ltd.
    Expert of ePOS solutions
    Website: http://www.itech.com.hk (IE only)
    Tel: (852)2325 3883 Fax: (852)2325 8288
  • wisestpotato

    #2
    Re: What's the difference between name and id?

    On 8 Mar, 08:55, Man-wai Chang <toylet.toy...@ gmail.comwrote:
    Why 2 reference points?
    >
    Id should be unique. That is, an id should apply to only one element
    within a page. Lots of elements can share the same "name" however.
    This is why the DOM gives you "getElementById (SomeId)" to return a
    single element, and "getElementsByN ame(SomeName)" to return a
    collection of elements.

    wp.

    Comment

    • VK

      #3
      Re: What's the difference between name and id?

      On Mar 8, 11:55 am, Man-wai Chang <toylet.toy...@ gmail.comwrote:
      What's the difference between name and id?
      Why 2 reference points?
      name is coming from the initial DOM 0 (DOM Zero Level) interface
      model. In this model form controls are identified by name or by index
      in elements collection:
      document.forms[0].elements[0];
      document.forms[0].elements["FirstContr ol"];
      Only controls having name attribute set are being submitted, this rule
      is in effect since then and forever.

      id attribute was introduced only in DOM 1. As other poster pointed out
      ID supposed to be unique for the given document, while name can be the
      same for many elements (think of radio buttons group for instance).

      This is why say accesskey is attached to ID and not to name. That is
      not the only neither the main reason but it gives the idea why
      sometimes it is useful to set both id and name for a form control,
      like:

      <input type="radio" value="yes" name="rdg" id="rd01">
      <label for="rd01">Yes</label>

      <input type="radio" value="no" name="rdg" id="rd02">
      <label for="rd02">No</label>


      For anchors and links collections name attribute usage is deprecated
      and rarely used.

      document.images["ImageName"] is still pretty much in use.

      Both from above are from the same DOM 0 model.

      P.S.Do you have a Gecko-only version to look at?



      Comment

      • Man-wai Chang

        #4
        Re: What's the difference between name and id?

        Id should be unique. That is, an id should apply to only one element
        within a page. Lots of elements can share the same "name" however.
        This is why the DOM gives you "getElementById (SomeId)" to return a
        single element, and "getElementsByN ame(SomeName)" to return a
        collection of elements.
        What would happen if the HTML has a duplicated id? Will
        getElementByNam e("duplicated_i d") fail or just return the first one it
        found?

        --
        .~. Might, Courage, Vision, SINCERITY. http://www.linux-sxs.org
        / v \ Simplicity is Beauty! May the Force and Farce be with you!
        /( _ )\ (Ubuntu 6.10) Linux 2.6.20.1
        ^ ^ 20:59:01 up 11 days 4:40 0 users load average: 1.05 1.01 1.00
        news://news.3home.net news://news.hkpcug.org news://news.newsgroup.com.hk

        Comment

        • RobG

          #5
          Re: What's the difference between name and id?

          On Mar 8, 8:23 pm, "VK" <schools_r...@y ahoo.comwrote:
          [...]
          id attribute was introduced only in DOM 1.
          "Only"? DOM 0 didn't exist until there was a DOM 1.
          For anchors and links collections name attribute usage is deprecated
          and rarely used.
          Wrong. The name attribute is not deprecated for A elements (which can
          be either anchors or links) in HTML 4. An anchor is *defined* as an A
          element with a name - if the name attribute for A elements is
          deprecated, then so are anchors.

          Both from above are from the same DOM 0 model.
          They are from the same HTML specification, they are incidental to
          javascript.


          --
          Rob

          Comment

          • RobG

            #6
            Re: What's the difference between name and id?

            On Mar 8, 6:55 pm, Man-wai Chang <toylet.toy...@ gmail.comwrote:
            Why 2 reference points?
            Oh, you mean why both id and name? This is a javascript forum, where
            you might get a good answer but it is not this group's speciality.
            Name and ID belong to HTML, so ask in a group dedicated to that topic:

            news:comp.infos ystems.www.authoring.html
            <URL: http://groups.google.com/group/comp....authoring.html
            >
            You might also like to look at the HTML specification for all the
            elements that can have a name attribute and what it means for each:

            <URL: http://www.w3.org/TR/html4/index/attributes.html >

            Expert of ePOS solutions
            As J said in Men In Black:

            "Unlimited technology from the whole universe, and
            we're crusin' around in a Ford P-O-S."

            :-)

            --
            Rob


            Comment

            • RC

              #7
              Re: What's the difference between name and id?

              Man-wai Chang wrote:
              >
              Why 2 reference points?
              >
              Just think in real life.
              Two different people may be have the
              same name, but their driver license number (ID)
              are not the same.

              In a file you can not have duplicate ID numbers.

              getElementById( SomeId)

              should fail or only get the first one
              or last one

              Comment

              • Tim Slattery

                #8
                Re: What's the difference between name and id?

                Man-wai Chang <toylet.toylet@ gmail.comwrote:
                >
                >Why 2 reference points?
                IMHO, "name" was intended to be used to identify form fields. When a
                form is submitted the "name" attribute is used to identify each item
                of data transmitted. Since there can be more than one form on a page,
                a "name" does not have to be unique within a page. Moreover, multiple
                fields - in particular radioboxes - will have the same name attribute
                in the same form.

                The ID attribute is intended to uniquely identify an element - ANY
                element, whether part of a form, or a paragraph, span, list item, div,
                anything at all - in a form. The expectation is that values of id will
                be unique within a form.

                So...document.g etElementByName () returns an array, since it assumes
                that there may be several elements with the same name. But
                document.getEle mentById() returns a single value, since it assumes
                that ID will be unique. If there is more than one element with the
                requested ID, the function returns the first one.

                --
                Tim Slattery
                Slattery_T@bls. gov

                Comment

                • VK

                  #9
                  Re: What's the difference between name and id?

                  On Mar 8, 4:23 pm, "RobG" <r...@iinet.net .auwrote:
                  For anchors and links collections name attribute usage is deprecated
                  and rarely used.
                  >
                  Wrong. The name attribute is not deprecated for A elements (which can
                  be either anchors or links) in HTML 4. An anchor is *defined* as an A
                  element with a name - if the name attribute for A elements is
                  deprecated, then so are anchors.
                  My bad!

                  Comment

                  • Tim Slattery

                    #10
                    Re: What's the difference between name and id?

                    Tim Slattery <Slattery_T@bls .govwrote:

                    >The ID attribute is intended to uniquely identify an element - ANY
                    >element, whether part of a form, or a paragraph, span, list item, div,
                    >anything at all - in a form.
                    I meant "in a PAGE", of course. Arghhh!

                    --
                    Tim Slattery
                    Slattery_T@bls. gov

                    Comment

                    • Man-wai Chang

                      #11
                      Re: What's the difference between name and id?

                      Thanks to everyone.

                      --
                      iTech Consulting Co., Ltd.
                      Expert of ePOS solutions
                      Website: http://www.itech.com.hk (IE only)
                      Tel: (852)2325 3883 Fax: (852)2325 8288

                      Comment

                      Working...