26

I'm busy with something for school in HTML 5.

So here is my bit of code

<label for="name">Name</label>
<input type="text" id="name" name="name" placeholder="your name" required><br>

So my question actually is:

What is the difference between the NAME and the ID? purpose? which one is more important?

Kendrick
  • 3,657
  • 1
  • 17
  • 39
Nick Zijlstra
  • 317
  • 2
  • 6
  • 14

3 Answers3

54

In short, the name is the identifier that is sent to the server when you submit the form. The id is a unique identifier for the browser, clientside, for javascript and such.

Joe Frambach
  • 25,568
  • 9
  • 65
  • 95
20

The name attribute is for submitting a form element to the server; many elements may share the same name (e.g. radio buttons, which must have the same name within the set).

The id attribute is for uniquely identifying any element (not just form elements). It must be unique throughout the entire document.

Phrogz
  • 271,922
  • 98
  • 616
  • 693
  • 2
    And: A common, good practice to prevent brain-overload is to use the same value for the id and the name, when both are needed. (And, use that same name as variables in php/javascript programs that hold denoting the node or its value, and also keep it similar to the label shown on the page.) For radio-buttons (where the name is shared across several nodes/ids), use obviouslycorresponding names, eg `name="buttons" id="buttons-yes"`. – not-just-yeti Jul 20 '14 at 17:01
8

The id attribute is supposed to be unique in your document. Only one element can have a given id. document.getElementById() finds the first element with the given id.

The name attribute is used by forms as the key in a key/value pair when submitting the form. The value attribute is both displayed in the browser, and submitted with the form.

Neither is "more important," they're just different. If you have an XML mindset, they're both just attributes on a node. In HTML they have more meaning though.

Tony R
  • 10,116
  • 21
  • 70
  • 100