+ 1
What is the purpose of the attribute 'id'?
In some places, this 'id' attribute is used like text boxes. What kind of function it does?
2 Réponses
+ 5
* HTML
In HTML the element's Id attribute is used to identify a distinct element from the others. In a single HTML file there can only exists one element identified by the Id attribute value, so it's a unique identifier. There's also the class attribute, which can be used for multiple elements.
e.g.
<p id="uniqueId">Some content</p>
<p class="className">Some content</p>
* CSS
In CSS point of view, an HTML element identifier is used to apply a certain style, or behavior. CSS applies the rules for styles and behavior based on two categories, based on Id (identifier) and/or class (classification).
When a developer defines a CSS style or behavior for HTML element by Id the style or behavior is applied to the single element. Contrary when the style or behavior is defined based on class the style or behavior is applied to all HTML elements belonging to the said class.
e.g.
#uniqueId
{
color:#c0c0c0;
}
.className
{
color:#d0d0d0;
}
* JavaScript
In JavaScript a HTML element's Id is used to identify a unique element whose content, CSS style, or behavior is to be manipulated. JavaScript uses functions to manipulate the HTML element's style or behavior. The getElementById function is used to obtain a reference to a HTML element by its identifier, and the getElementsByClassName function is used to obtain references to all elements belonging to a specific CSS class.
e.g
var uniquePar = getElementById("uniqueId");
var multiPars = getElementsByClassName("className");
I assume you are in HTML course and probably haven't got to CSS & JavaScript course, but once you finished these three you'll understand what I'm writing here. Sorry if I put too much lines here, I just feel like writing right now.
Hth, cmiiw
+ 4
the use of the id attribute is needed to access this element. You can change its properties, parameters, change the text or change style. And just hide or show its element.