3

When you submit a form, does the form look for both "name" and "id" values? What does the "id" do other than styling it with CSS?

<select name="combobox" id="combobox">
   <option value="0">Inactive</option>
   <option value="1">Active</option>
</select>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
learntosucceed
  • 1,109
  • 4
  • 11
  • 16
  • 1
    Here's an answer: http://stackoverflow.com/questions/4487860/name-vs-id-attribute-in-html – jpaugh Apr 28 '13 at 04:02
  • [List of uses of the id attribute in HTML.](http://stackoverflow.com/questions/13001236/why-should-one-add-id-to-their-html-tags/13001519#13001519) – Alohci Apr 28 '13 at 09:34
  • @jpaugh This specifically asks in the context of
    , not the whole concept of id and name attributes and their usages.
    – Silidrone Oct 07 '20 at 13:52
  • Related: *[Difference between id and name attributes in HTML](https://stackoverflow.com/questions/1397592)* – Peter Mortensen Aug 07 '21 at 23:35

5 Answers5

7

Name

The name attribute is needed for the database or other data destination to uniquely identify that piece of data.

When the form is submitted, most scripts use the name attribute to place the form data into a database or into an email that can be read by a person. Thus, if the <input> element is for the site visitor to enter their name into, then the name attribute would be name="name" or name="first-name", etc. (source (W3C)).

A control's "control name" is given by its name attribute. The scope of the name attribute for a control within a FORM element is the FORM element. Source (W3C).

Id

The id attribute has several roles in HTML:

  • As a style sheet selector.
  • As a target anchor for hypertext links.
  • As a means to reference a particular element from a script.
  • As the name of a declared OBJECT element.
  • For general purpose processing by user agents (e.g., for identifying fields when extracting data from HTML pages into a database, translating HTML documents into other formats, etc.) (Source (W3C).)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

name indicates that key will be used to send the value.

id is typically used so that JavaScript can find the element.

For convenience, they may be the same, but not necessarily.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

When you submit a form, does the form look for both "name" and "id" values?

No, just the name attribute

What does the "id" do other than styling it with CSS?

You can use the id to reference the element in the form elements collection.

Say we have a form element referenced by the variable form, you can access the element (form elements, input , selects, etc.) in the form. With id, say combobox, then you can access this element via form['combobox'] or form.combobox . This is also true for the name attribute - http://jsfiddle.net/YQRtR/.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Musa
  • 96,336
  • 17
  • 118
  • 137
  • Re *"...reference the element in the form elements collection"*: In what context and from where? JavaScript / DOM? On the server? On the client? Can you clarify in your answer? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 07 '21 at 23:09
0

name will appear in the query string, i.e.,

?name=value

So in your case:

?combobox=0 OR ?combobox=1

depending on the user's selection.

id is used by CSS to add styles, similar to class except a specific id can only be assigned to one DOM element. The id can also be used to manipulate the element on the DOM using JavaScript, as it allows the element to be identified.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HennyH
  • 7,794
  • 2
  • 29
  • 39
0

You can refer to developer.mozilla.org or http://htmldog.com/ for details about HTML.

id

The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document).

The id attribute is most used to point to a style in a style sheet, and by JavaScript (via the HTML DOM) to manipulate the element with the specific id.

name

The name attribute specifies the name of a form.

The name attribute is used to reference elements in JavaScript code, or to reference form data after a form is submitted.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vaichidrewar
  • 9,251
  • 18
  • 72
  • 86