NEW

6/recent/ticker-posts

Header Ads Widget

HTML Id Attribute

 

HTML Id Attribute

The id attribute is used to specify the unique ID for an element of the HTML document. It allocates the unique identifier which is used by the CSS and the JavaScript for performing certain tasks.

Note: In the Cascading Style sheet (CSS), we can easily select an element with the specific id by using the # symbol followed by id.

Note: JavaScript can access an element with the given ID by using the getElementById() method.

Syntax

  1. <tag id="value">  

Example 1: The following example describes how to use the id attribute in CSS document:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <title>  
  5. Example of Id attribute in CSS  
  6. </title>  
  7. <style>  
  8. #Cars {  
  9. padding: 40px;  
  10. background-color: lightblue;  
  11. color: black;      
  12. text-align: center;  
  13. }   
  14.   
  15. #Bikes  
  16. {  
  17. padding: 50px;  
  18. background-color: lightGreen;  
  19. text-align: center;  
  20. }  
  21. </style>  
  22. </head>  
  23. <body>  
  24. <p> Use CSS to style an element with the id: </p>  
  25. <h1 id="Cars"> Cars </h1>  
  26. <h1 id="Bikes"> Bikes </h1>  
  27. </body>  
  28. </html>   

Output:

HTML Id Attribute

Example 2: The following example describes how to use the ID attribute in JavaScript.

  1. <!DOCTYPE html>  
  2. <html>   
  3. <head>   
  4. <title> Date Attribute </title>   
  5. <script>   
  6. function viewdate() {   
  7. var x = document.getElementById("dob").value;   
  8. document.getElementById("demo").innerHTML = x;   
  9. </script>   
  10. </head>   
  11. <body>   
  12. Employee Name: <input type="text" placeholder="Your Good name"/>   
  13. <br>  
  14. <br>  
  15. Date of Joining:   
  16. <input type="date" id="dob">  
  17. <br>   
  18. <button onclick="viewdate()"> Submit   
  19. </button>   
  20. <br>  
  21. <h2 id="demo"> </h2>   
  22. </body>   
  23. </html>  
Test it Now

Output:

HTML Id Attribute

Post a Comment

0 Comments

Recents Post

Exploring Paidwork - Your Gateway to Earning Online