www.webdeveloper.com
Results 1 to 7 of 7

Thread: Difference between NAME and ID

  1. #1
    Join Date
    Apr 2011
    Posts
    3

    Question Difference between NAME and ID

    Hello again

    I was wondering if some one could clearly explain the difference between ID and NAME attributes in html tags. And which one is used with scripting language such as JavaScript and PHP to refer to.

    Thanks a lot

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    That's a little hard to explain. Names have been around longer than IDs and names work differently depending upon what they are naming. Even what counts as a valid name depends upon what is being named. And names aren't necessarily unique. IDs, however, are nice and consistent and must be unique--which isn't always good.

    Scripting-wise which you use depends upon what you are doing and how. If you are using a server side script to process a form then you'll be wanting to use some names. Client side scripting uses names in DOM level 0 and IDs in DOM level 1.

    Just be careful, some browsers get a little weird when you give the same element a name and an ID that aren't the same.

    See http://www.w3.org/TR/html401/index/attributes.html .
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  3. #3
    Join Date
    Apr 2011
    Posts
    3
    Just be careful, some browsers get a little weird when you give the same element a name and an ID that aren't the same.
    Do you mean if I want to give an element both ID and NAME attributes, their values must be the same?

    Thanks a lot.

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Quote Originally Posted by Mahdiem View Post
    Do you mean if I want to give an element both ID and NAME attributes, their values must be the same?
    Yes, but that get's interesting because what counts as a legal name is different from what counts as legal ID. Best to just use one or the other. I generally stick with IDs except where I need names id est for forms and meta data.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  5. #5
    Join Date
    Apr 2011
    Posts
    3
    Amazing stuff.
    Thanks a lot. You have been very helpful.

  6. #6
    Join Date
    Nov 2002
    Location
    Flint, Michigan, USA
    Posts
    593
    I always use both name and id and make them match except when they are required to be different. This makes the page work properly in DOM0 and DOM1.

    One time when they must be different is for radio buttons. The name attribute groups them and the id identifies individual buttons.
    Please give me a hand at http://www.gofundme.com/bkzr98.

  7. #7
    Join Date
    Mar 2011
    Location
    Ponca City, OK
    Posts
    42
    I general rule is:

    id is to define what styling to apply to the object with CSS. Or to use with JS
    name provides identifiers for say POST processing

    For radio buttons and check boxes, all the radios or check boxes in a group share the same name, and have different value tags.

    For select elements, the select gets a name, while the options get values.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles