HTML | DOM Code Object
The DOM Code Object is used to represent the HTML <code> element. The Code element is accessed by getElementById().
Syntax:
document.getElementById("id");
Where ‘id’ is the ID assigned to the code tag.
Example-1:
<!DOCTYPE html> <html> <body> <center> <h1 style = "color:green; font-size:38px;"> GeeksForGeeks </h1> <h2>DOM code Object</h2> <!-- Assigne id to CODE tag--> <code id="GFG"> GeeksForGeeks: A computer science portal for geeks. </code> <br> <br> <button onclick="myGeeks()">Submit</button> <script> function myGeeks() { var g = document.getElementById("GFG"); <!--Change the color of code element.--> g.style.color = "coral"; } </script> </body> </html> |
Output:
Before Clicking on Button :

After Clicking On Button:

Example-2: Code Object can be created by using the document.createElement method.
<!DOCTYPE html> <html> <body> <center> <h1 style = "color:green; font-size:38px;" > GeeksForGeeks </h1> <h2>DOM code Object</h2> <br> <button onclick="myGeeks()">Submit</button> <script> <!--Creating code element--> function myGeeks() { var g = document.createElement("CODE"); var f = document.createTextNode("GeeksForGeeks:" +"A computer science portal for geeks."); g.appendChild(f); document.body.appendChild(g); } </script> </body> </html> |
Output:
Before Clicking On Button:

After Clicking On Button:

Supported Browsers: The browser supported by DOM Code Object are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- HTML | <code> Tag
- HTML | Computer Code Elements
- HTML | DOM KeyboardEvent code Property
- HTML | DOM Object Object
- HTML | DOM li Object
- HTML | DOM Del Object
- HTML | DOM DT Object
- HTML| DOM HR Object
- HTML | DOM Map Object
- HTML | DOM Nav Object
- HTML | DOM S Object
- HTML | DOM Pre Object
- HTML | DOM Ol Object
- HTML | DOM Kbd Object
- HTML| DOM Ins Object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



