Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the "id" in an input tag necessary?

<p>
  <input type="text" id="search" name="keywords" />
  <input type="submit" value="Search" name="Submit" />
</p>

For the above code I was getting validation errors, but once I removed the id="search" the validation was good and error-free. I thought you needed an id, but I'm wondering if it is supposed to be there?

like image 979
Holly Avatar asked Jun 15 '09 19:06

Holly


People also ask

Do inputs need IDS?

You do not need the ID attribute. The name attribute is the one that gets passed.

Why do we use id in input tag?

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.

Is name attribute necessary in input tag?

Name is not a required attribute. A small quote from the HTML spec: The name content attribute gives the name of the form control, as used in form submission and in the form element's elements object. If the attribute is specified, its value must not be the empty string.


2 Answers

Do you have any other elements with that id in the document? That would be the only reason for validation to fail. IDs are meant to be unique in the document, if you have it elsewhere it would be invalid.

IDs are good when you plan on doing some sort of client-side work on the element, as an element that has an ID can easily and quickly be retrieved by Javascript. It is also good when you are using <label> elements, as you can then use the for attribute (which takes an ID) to point to the field.

Other than that, it doesn't really matter.

like image 165
Paolo Bergantino Avatar answered Sep 20 '22 00:09

Paolo Bergantino


You do not need the ID attribute. The name attribute is the one that gets passed.

like image 45
Daniel A. White Avatar answered Sep 18 '22 00:09

Daniel A. White