The DOM dl object is used to represent the HTML <dl> element. The dl element can be accessed using the getElementById() method. The dl is used to create a description list in HTML.
Syntax:
document.getElementById("id");
Where ‘id’ is the ID assigned to the dl tag.
Example 1: In the below program the dl element is accessed and the color of the text inside the description list is changed.
HTML
<!DOCTYPE html>
< html >
< body >
< h1 style = "color:green;" >
GeeksForGeeks
</ h1 >
< h2 >DOM dl Object</ h2 >
< dl id = "id" >
< dt >Sorting</ dt >
< dd >Merge sort</ dd >
</ dl >
< button onclick = "Geeks()" >Click Here!</ button >
< p id = "p" style = "color:green" ></ p >
< script >
function Geeks() {
let doc = document.getElementById("id").innerHTML;
document.getElementById("p").innerHTML = doc;
}
</ script >
</ body >
</ html >
|
Output:
Example 2: DL Object can be created by using the document.createElement method.
HTML
<!DOCTYPE html>
< html >
< body >
< h1 style = "color:green;" >
GeeksForGeeks
</ h1 >
< h2 >DOM dl Object</ h2 >
< button onclick = "Geeks()" >Click Here!</ button >< br >
< script >
function Geeks() {
// Creating a dl element
let doc = document.createElement("DL");
doc.setAttribute("id", "dl");
document.body.appendChild(doc);
// Creating a DT element
let doc1 = document.createElement("DT");
let txt1 = document.createTextNode("Sorting");
doc1.appendChild(txt1);
doc1.setAttribute("id", "dt");
document.getElementById("dl").appendChild(doc1);
// Creating a dd element
let doc2 = document.createElement("DD");
let txt2 = document.createTextNode("Merge sort");
doc2.appendChild(txt2);
document.getElementById("dl").appendChild(doc2);
}
</ script >
</ body >
</ html >
|
Output:
Supported Browsers:
- Google Chrome
- Mozilla Firefox
- Edge
- Safari
- Opera
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!