views:

642

answers:

3
<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?

+1  A: 

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

Daniel A. White
This is incorrect. IT is actually the other way around: http://www.devguru.com/technologies/xhtml/QuickRef/xhtml_attribute_id.html
Rob Allen
+9  A: 

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.

Paolo Bergantino
The label target is the main reason I use them. I hate hate hate websites that make me click ON the radio button or checkbox.
Mark Hurd
No there's nothing else with that id. I thought that was the case, but wasn't 100% sure. So I just removed it and it's fine. Thank you!
Holly
A: 

Daniel is correct. A label's for attribute is associated with an input's name attribute. In this way, if you select a label with for="first_name", it will select the input with name="first_name".

Nathan