0

What is "id" used for in Html

21st May 2018, 7:05 AM
Christian Addison
5 Réponses
+ 1
Sometime we need to refer to ONE particular element in our html page and while there are most ways to refer to an element, using "id" is the faster and most logical one... "id" mean identificative and you can set it to any element but you cannot set same "id" to different element ("id" must identify univocally elements) and one "id" for element is allowed... Another popular and suggested way to refer to (MULTIPLE) elements is the "class" attribute that dont define an identification but you can think like it define a group which elements are related by something (usually style)... Because of this same "class" attribute can be assigned to multiple elements and any element can has multiple "class" attributes <p id="myFirstP">I am first p</p> <p id="mySecondP">I am the second sigh ..</p> Like you see you have two paragraphs with id setted (note they are different)... Now you can refer in fast way from HTML: <a href="#myFirstP" >Go to my first paragraph</a> from CSS: #myFirstP{ color: green; } from Javascript: var second_p= document.getElementById("mySecondP");
21st May 2018, 7:56 AM
KrOW
KrOW - avatar
+ 1
id is like to assign a unique number or character to any class by which we can use that id alternative of name
21st May 2018, 7:20 AM
MsJ
MsJ - avatar
+ 1
Please give me an example
21st May 2018, 7:22 AM
Christian Addison
+ 1
<p id=test>red</p> <style> #test{ color:redb } </style> The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document). The id value can be used by CSS and JavaScript to perform certain tasks for a unique element with the specified id value. In CSS, to select an element with a specific id, write a hash (#) character, followed by the id of the element as above
21st May 2018, 7:38 AM
Sudarshan Rai
Sudarshan Rai - avatar
0
what will be ur output
21st May 2018, 7:40 AM
Christian Addison