The HTML id
attribute is used in assigning a unique identity to any element in a webpage. Just any element can have an identity in webpage.
as attributes with values are basically assigned to HTML elements so as to help us have access to such elements and perform styling or scripting on them. Therefore, it is not necessary to assign an id
to all HTML elements but rather to those that we may want to access for styling or scripting purposes.
var btn = document.getElementById("btn");
btn.innerText = "CLICKED";
alert("btn clicked upon");
Copy PRACTICE IT
Above, we created three (3) elements in a webpage and assigned a unique id (btn
- The id attribute gives a unique identity to an HTML element in a webpage.
- Through this unique identity, we can therefore access such elements and apply both styling and scripting on them. This is just as a couple give unique names to their children so as to call on them at any time.