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: In this example, we will use DOM Code Object
HTML
<!DOCTYPE html>
< html >
< body >
< h1 style = "color:green; font-size:38px;" >
GeeksForGeeks
</ h1 >
< h2 >DOM code Object</ h2 >
< code id = "GFG" >
GeeksForGeeks: A computer science portal for geeks.
</ code >
< br >
< br >
< button onclick = "myGeeks()" >Submit</ button >
< script >
function myGeeks() {
let g = document.getElementById("GFG");
g.style.color = "coral";
}
</ script >
</ body >
</ html >
|
Output:
Example 2: Code Object can be created by using the document.createElement method.
HTML
<!DOCTYPE html>
< html >
< body >
< h1 style = "color:green; font-size:38px;" >
GeeksForGeeks
</ h1 >
< h2 >DOM code Object</ h2 >
< br >
< button onclick = "myGeeks()" >Submit</ button >
< script >
function myGeeks() {
let g = document.createElement("CODE");
let f =
document.createTextNode("GeeksForGeeks:"
+ "A computer science portal for geeks.");
g.appendChild(f);
document.body.appendChild(g);
}
</ script >
</ body >
</ html >
|
Output:
Supported Browsers: The browser supported by DOM Code Object are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Last Updated :
14 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...