The selection.html() function is used to set the inner HTML on all the selected elements. If the value is constant then all the elements are given same value. A null value will clear the content of the element.
Syntax:
selection.html([value]);
Parameters: This function accepts single parameter as mentioned above and described below:
- Value: It is of type string that sets the HTML to the document.
Return Value: This function does not return anything.
Example 1: In the following code, the HTML “p” element content is changed to “bold” text.
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" path1tent= "width=device-width, initial-scale=1.0"> </script> <style> p { background-color: #f2f2f2; padding: 10px; width: 200px; line-height: 5px; } p:hover { background-color: grey; padding: 10px; cursor: pointer; } </style> </head> <body> <div> <h1 style="color:green;"> GeeksforGeeks </h1> <h4>D3.js selection.html() Function</h4> <p>Click Here!</p> </div> <script> function func() { // Selecting all p and setting // the innerHTML of the p var chk = d3.selectAll("p") .html("<b>This is from .html</b>"); var text = document.querySelector("p"); } let btn = document.querySelector("p"); btn.addEventListener("click", func); </script> </body> </html> |
Output:

Example 2: The following example clears the content using null.
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" path1tent= "width=device-width,initial-scale=1.0"> </script> <style> p { background-color: #f2f2f2; padding: 10px; width: 200px; line-height: 5px; } p:hover { background-color: grey; padding: 10px; cursor: pointer; } </style> </head> <body> <div> <h1 style="color:green;"> GeeksforGeeks </h1> <h4>D3.js selection.html() Function</h4> <p><b>Click Here</b></p> </div> <script> function func() { // Selecting p and setting the // innerHTML of the p var chk = d3.selectAll("p") .html(null); var text = document.querySelector("p"); } let btn = document.querySelector("p"); btn.addEventListener("click", func); </script> </body> </html> |
Output:

Recommended Posts:
- How to get the function name from within that function using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to Check a Function is a Generator Function or not using JavaScript ?
- PHP | imagecreatetruecolor() Function
- p5.js | year() function
- D3.js | d3.utcTuesdays() Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- D3.js | d3.map.set() Function
- D3.js easePolyOut Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- D3.js | d3.bisectLeft() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
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.


