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

How Can I Clear a Div\'s Content Using JavaScript?

Linda Hamilton
Release: 2024-11-30 18:52:16
Original
389 people have browsed it

How Can I Clear a Div's Content Using JavaScript?

Clearing Div Content with JavaScript

To dynamically remove the content of a div element when a user clicks a button, JavaScript offers an efficient solution.

Answer:

Using Pure JavaScript:

  1. Define a function to clear the content of a div:
function clearBox(elementID) {
    document.getElementById(elementID).innerHTML = "";
}
Copy after login
  1. Add an onclick event listener to the button that triggers the clearBox function:
<button onclick="clearBox('cart_item')">Clear Div</button>
Copy after login

Using jQuery (for comparison):

$("#cart_item").html("");
Copy after login

The above is the detailed content of How Can 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