Home >Web Front-end >CSS Tutorial >How Do I Clear a DIV\'s Content Using JavaScript?
Clearing Content of a DIV with JavaScript
In many web pages, there are instances where you want to clear the contents of a specific element, such as a DIV. Here's how to achieve this:
To clear the content of a DIV using JavaScript, you can use the innerHTML property. Here's how:
function clearBox(elementID) { document.getElementById(elementID).innerHTML = ""; }
This function receives the ID of the DIV you want to clear and sets its innerHTML property to an empty string, effectively clearing its contents.
To use this function, add an event listener to the button you want to control the DIV clearing. For example:
<button onclick="clearBox('my_div')">Clear</button>
When the "Clear" button is clicked, the clearBox() function will be invoked, and the DIV with the ID "my_div" will have its content cleared.
The above is the detailed content of How Do I Clear a DIV\'s Content Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!