when use ID and when use NAME in a HTML tag ??

Discussion in 'Javascript' started by Ton den Hartog, Nov 30, 2003.

  1. Ton den Hartog, Nov 30, 2003
    #1
    1. Advertising

  2. Ton den Hartog wrote:

    > I am confused, when use ID and when use NAME to identify an element in HTML
    > ?


    Well, check the HTML 4 specification
    http://www.w3.org/TR/html4/
    and if you have further questions ask in a newsgroup about HTML.
    The id attribute is defined on (nearly) all elements and needs to be
    unique in the whole document while the name attribute is defined for
    elements like <input>, <textarea>, <select> where on form submission the
    name of the element is paired with its value and sent to the server.
    The name attribute is also defined on some elements like <img>, <form>,
    <applet> back from pre HTML 4 times and still has some usefulness if you
    want to script such elements in older browsers like Netscape 4.
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Nov 30, 2003
    #2
    1. Advertising

  3. "Ton den Hartog" <> writes:

    > I am confused, when use ID and when use NAME to identify an element in HTML
    > ?


    According to the HTML 4 specification, "id" is recommended for most
    elements where "name" is allowed.

    Only on form controls ("input", "select", "button", "textarea", and
    possibly "object") and the "param" and "meta" elements are "name"
    important. On form controls elements, the "name" attribute value
    specifies the "control name". In a "param" element it gives tha name
    of the parameter. In a "meta" tag, it gives the "metainformation name",
    whatever that is. If you have both "name" and "id" on such elements,
    the "id" gives the globally unique identifier for the element, while
    the "name" has a different meaning.

    On all other elements that allow a name attribute ("a", "applet",
    "form", "frame", "iframe", "img", and "map"), use "id". If you have
    both "id" and "name" attributes on such an element, their values must
    be equal. <URL:http://www.w3.org/TR/html4/struct/links.html#h-12.2.3>

    /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, Nov 30, 2003
    #3
  4. > I am confused, when use ID and when use NAME to identify an element in HTML
    > ?


    name is used to annotate POST data in forms. id is used to identify elements for
    scripting and styling. Some browsers used them interchangeably, which is
    confusing.

    http://www.crockford.com
     
    Douglas Crockford, Nov 30, 2003
    #4
  5. JRS: In article <3fc9fddf$0$1489$4all.nl>, seen in
    news:comp.lang.javascript, Ton den Hartog <ton.den.hartog.removespam@ton
    h.net> posted at Sun, 30 Nov 2003 15:25:31 :-

    >I am confused, when use ID and when use NAME to identify an element in HTML
    >?


    Since ID is required to be unique within the document, whilst NAME is or
    can be limited in scope, then use NAME in preference to ID when you have
    the choice.

    Until recently, the page on which I am currently working used a
    different NAME for every element, and every element was created by a
    separate piece of HTML, which was satisfactory.

    Now that more of the contents are computed rather than typed in, and the
    page source is 20% smaller, it is convenient to use identical NAMEs for
    matching items in Form A & Form B; the form NAME distinguishes between
    them as necessary.

    However, ID has one advantage : an editor can easily check that an ID is
    unique within a straightforward page, and an ID can be located, easily
    but not efficiently, by a search over all IDs.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
     
    Dr John Stockton, Nov 30, 2003
    #5
  6. Ton den Hartog

    Frank Carr Guest

    "Douglas Crockford" <> wrote in message
    news:b9da8$3fca2040$44a4afc0$...

    > Some browsers used them interchangeably, which is
    > confusing.


    I got bit by this one myself today. See my thread "Image Load in IE vs.
    Mozilla".

    Is there a good web site that compares these kinds of browser differences?
    I'm in the process of moving from doing a lot of backend and database
    development in VB/ASP/SQL to doing some front end Javascript programming so
    anything along this line would be helpful.

    --
    Frank Carr

    http://www15.brinkster.com/vbnotebook
     
    Frank Carr, Nov 30, 2003
    #6
  7. Ton den Hartog

    F. Da Costa Guest

    A while ago I printed (yep it still exists) a piece (and its still on my
    desk) called:
    Incompatibilities in IE and Netscape: HTML & Javascript.

    Not sure whether its usefull.
    Link: http://4guysfromrolla.com/webtech/011200-1.shtml

    Cheers
    Fermin DCG

    Frank Carr wrote:
    > "Douglas Crockford" <> wrote in message
    > news:b9da8$3fca2040$44a4afc0$...
    >
    >
    >>Some browsers used them interchangeably, which is
    >>confusing.

    >
    >
    > I got bit by this one myself today. See my thread "Image Load in IE vs.
    > Mozilla".
    >
    > Is there a good web site that compares these kinds of browser differences?
    > I'm in the process of moving from doing a lot of backend and database
    > development in VB/ASP/SQL to doing some front end Javascript programming so
    > anything along this line would be helpful.
    >
     
    F. Da Costa, Dec 1, 2003
    #7
  8. "F. Da Costa" <> writes:

    > Incompatibilities in IE and Netscape: HTML & Javascript.
    >
    > Not sure whether its usefull.
    > Link: http://4guysfromrolla.com/webtech/011200-1.shtml


    It's comparing IE 4 to Netscape 4. I would say it's long past it's
    expiration date.

    (Please don't top post)
    /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, Dec 1, 2003
    #8
  9. Ton den Hartog

    F. Kolb Guest

    "Ton den Hartog" <> wrote in message news:<3fc9fddf$0$1489$4all.nl>...
    > I am confused, when use ID and when use NAME to identify an element in HTML
    > ?
    >
    > Ton den Hartog


    It really is simply as that:

    WHEN using the DOM-Model... and you want to access several elements
    with the same NAME (checkboxes / radio-buttons for example)
    you HAVE TO use the name-attribute because there only exists the
    "document.getElementsByName" function which IS handled STRICT in
    NS/Mozilla-based browsers and "loosely" (id-attribute works too) by IE
    browsers.

    my personal opinion about this is:
    W3C caused this confusion by entering a "getElementById" to retrieve
    info from a UNIQUE element and to retriev info from MORE elements
    sharing the same NAME a "getElemntsByName" !!! ...really NOT that
    "Standard"-like.

    so long...
     
    F. Kolb, Dec 2, 2003
    #9
  10. Ton den Hartog

    GIMME Guest

    ID is used in DHTML to hide and place globs of html.

    name is part of a name-value pair that gets submitted to a server
    when there is a submit.


    "Ton den Hartog" <> wrote in message news:<3fc9fddf$0$1489$4all.nl>...
    > I am confused, when use ID and when use NAME to identify an element in HTML
    > ?
    >
    > Ton den Hartog
     
    GIMME, Dec 2, 2003
    #10
    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. RC
    Replies:
    2
    Views:
    451
    Malte
    May 10, 2005
  2. shruds
    Replies:
    1
    Views:
    893
    John C. Bollinger
    Jan 27, 2006
  3. RC
    Replies:
    2
    Views:
    676
  4. Replies:
    1
    Views:
    411
    Andrew Thompson
    Nov 29, 2006
  5. Replies:
    7
    Views:
    509
    Max Erickson
    Mar 26, 2007
Loading...

Share This Page