In addition to the specification document provided by meow.
An id is a unique name. It can be used in a number of different contexts, but it is used to uniquely identify an element in a document. This identification can serve serveral purposes.
* It can provide an anchor to link to within a document.
* It can allow the element to be styled via CSS.
* It can allow the element to be accessed via the DOM (Document Object Model), through Javascript.
Via the first and second examples the id is always referenced with a pound sign (or hash mark, whichever term you prefer).
Say you want to link to a specific location within a document. This is done by giving the element you want to link to an id name.
<p>Some text here</p>
<p>Some more text</p>
<p id='anchorhere'>Yet more text</p>
Now imagine that each of these paragraphs are extrordinarily lengthy. You can link to a specific paragraph by writing up a hyperlink, (assuming this page is called some_page.html)
http://www.example.com/some_page.html#anchorhere. With this link the page will automatically scroll to the third paragraph.
If you also want to add some styles to that third paragraph, to make it stand out from the others you do that via the id selector in CSS.
p#anchorhere {
background: lightyellow;
padding: 10px;
border: 1px solid black;
}
Now its been styled as well, note that both methods make use of the hash mark.
In an input form the id takes on an additional meaning. Consider this example.
<input type='text' name='some_name' id='somename' />
I suppose that technically you should avoid underscores in id names, as support is bad for them in older browsers, this is because of CSS. But nothing is really stopping you from making the name and the id the same, as in...
<input type='text' name='some_name' id='some_name' value='some value' />
Modern browsers understand that just fine.
Now an additional element can be introduced.
<label for='some_name'>Some name:</label> the for attribute links this label directly to the <input /> element with a some_name id. When you put this in a page and click on the <label> element, you'll see that the <input> gains focus.
You might wonder why have an id attribute when there is a name attribute, the reasoning behind this is the id name should be unique, it should not be repeated and it has multiple applications. The name attribute can be repeated. As far as the specifications are concerned, IIRC, the name attribute is deprecated for everything with the exception of form input elements.
Finally, (at least as much as I can think of so far), you can use the id name in javascript.
<script type='text/javascript'>
alert(document.getElementById('some_name').value);
</script>
This snip of javascript will alert the current value of the 'some_name' <input> element.
HTH!
Regards,
Rich
--
[
http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design