SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    <input name="" id="" ?

    I have pages where I've got <input name="blah" id="blah" ... where the name and id are the same.

    Is it correct to have both name and id for an input element? Since I'm doing getElementById, would the id= be sufficient for all occurrences?

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,208
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    It doesn't matter if they are the same or not, but you normally need them both. The name attribute identifies the input for the form processing script, which the id associates the input with an accompanying <label> element, which has a "for=" attribute.

    E.g.

    Code:
    <label for="blah">Blah</label>
    <input name="blah" id="blah">
    The for="blah" / id="blah" associates the label with the input. That's handy for people using a screen reader (who can't "see" the relationships) and also means you can click on the Label to activate the input.

    Of course, the id on the input can also be used as a hook for JS and CSS.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,836
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you are going to use the same value for botha name and an id then you need to make sure they are on the same element. If one is on one tag and the other is on a different tag then Internet Explorer will become more confused than it normally is.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,208
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by felgall View Post
    If one is on one tag and the other is on a different tag then Internet Explorer will become more confused than it normally is.
    Is that just on older versions, or are the latest ones as dopey?

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    If you are going to use the same value for botha name and an id then you need to make sure they are on the same element. If one is on one tag and the other is on a different tag then Internet Explorer will become more confused than it normally is.
    Good to know. I'm adding the id= to existing name= on form elements, and they do match each other only, and not other elements.


Bookmarks

Posting Permissions

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