Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Location
    England
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Difference between name and id attributes

    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
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,225
    Thanks
    23
    Thanked 606 Times in 605 Posts
    The name attribute is used to mark an anchor or a bookmark in a page. It's used with <A> and <map> tags, but it my be on it's way out (??) The ID is a css selector use to id a unique html element.

    That's probably not an official definition, but that's how I see it.

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,678
    Thanks
    6
    Thanked 1,009 Times in 982 Posts
    The name attribute is actually deprecated for all elements other than form controls (input, select, textarea) and should therefore not be used anymore (except for the aforementioned ones). Technically, ID and name have the same purpose, they uniquely designate an element. However, unlike the ID attribute, the name attribute can’t be referenced in CSS (except with more complicated attribute selectors). PHP is using the name attribute of form controls (not the form element itself, however). With JS there are designated ways to address form controls so no need to use the name attribute. Above all, you can also address the ID with JS. For internal page anchors the ID attribute should be used, as name is deprecated.

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    yep, agree with vipstephan.

    A lot of people still use the name attribute in a form tag which is deprecated in (x)html.

    For example, this html will not pass the w3c validator.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
        "http://www.w3.org/TR/1998/REC-html40-19980424/strict.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
            <title></title>
        </head>
        <body>
            <form name="myform" action="qwert.php" method="post">
                <div>
                    <input type="text" name="txtUsername" >
                </div>
            </form>
        </body>
    </html>
    Last edited by bullant; 04-18-2011 at 01:22 AM.


  •  

    Posting Permissions

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