Home > Web Front-end > CSS Tutorial > How Do I Clear a DIV\'s Content Using JavaScript?

How Do I Clear a DIV\'s Content Using JavaScript?

Susan Sarandon
Release: 2024-12-19 10:44:16
Original
268 people have browsed it

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 = "";
}
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template