0

So, I've read recently some posts about HTML5 best practices and all of them have the following practice:

"Use id attribute instead of name attribute"

Is that right? I mean, how am I going to handle forms in PHP, for example, if not by inputs' name attribute?

j08691
  • 204,283
  • 31
  • 260
  • 272
  • 1
    This is just an excerpt. We don't know what is being discussed. If it's html5 - the talk is about client side programming, where you really should use `id`s instead of `name`s. But that doesn't mean there shouldn't be `name`s. – u_mulder Jun 08 '16 at 19:26
  • It depends on what it is being used for – Adam Buchanan Smith Jun 08 '16 at 19:27
  • This sounds like the advice for using anchor bookmarks. And what sources are telling you to do this with forms? Citations please. – j08691 Jun 08 '16 at 19:32

2 Answers2

0

Look here for reference:

Difference between id and name attributes in HTML

https://teamtreehouse.com/community/what-is-the-difference-between-id-and-name-attributes-in-form-elements

Names can be used for more elements, while ids are unique. This helps with styling multiple elements without repeating your css code.

Long story short, id is not meant for naming form elements. You use id to make it easier for CSS or JavaScript to handle that one particular element with the unique id.

Community
  • 1
  • 1
yarwest
  • 873
  • 8
  • 19
0

Whether you're using CSS or JavaScript/jQuery with your forms, an ID is handy for most inputs/elements because you can reference them like easily. Remember, ID's are unique:

<div id="example1"></div>

If you have an element like I mentioned above, you can always call it later by doing this:

var x = document.getElementById("example1").id;

along with many other ways.

Names can sometimes be tied to a variety of elements on the page, whereas there will always only by one item with that particular ID.

LatentDenis
  • 2,839
  • 12
  • 48
  • 99