How to remove text from a div in JavaScript

0 votes
Can i know How to remove text from a div in JavaScript?
Mar 11 in Java-Script by Ashutosh
• 23,230 points
74 views

1 answer to this question.

0 votes

You can remove text from a <div> in JavaScript using the following methods:

1. Set innerText or textContent to an empty string

document.getElementById("myDiv").innerText = "";

// OR

document.getElementById("myDiv").textContent = "";

2. Set innerHTML to an empty string (Removes all content including child elements)

document.getElementById("myDiv").innerHTML = "";

3. Remove only text nodes while keeping child elements

const div = document.getElementById("myDiv");

div.childNodes.forEach(node => {

    if (node.nodeType === Node.TEXT_NODE) {

        node.remove();

    }

});

answered Mar 11 by Sanvi

Related Questions In Java-Script

0 votes
1 answer

How to remove all child elements of a DOM node in JavaScript?

Hello @kartik, Use modern Javascript, with remove! const parent = ...READ MORE

answered Sep 21, 2020 in Java-Script by Niroj
• 82,840 points
2,346 views
0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello, You can produce the javascript file via ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,840 points
13,437 views
0 votes
1 answer

How to Get values from a specific user - queryset in Django?

Hello @kartik, You can get the list of Users ...READ MORE

answered May 28, 2020 in Java-Script by Niroj
• 82,840 points
11,111 views
0 votes
1 answer

How to find event listeners on a DOM node when debugging or from the JavaScript code?

Hii @kartik, It is possible to list all ...READ MORE

answered Jun 8, 2020 in Java-Script by Niroj
• 82,840 points
129,102 views
0 votes
1 answer

How to Center a Single-Column Div Within a 12-Column Container in Bootstrap 3?

Using Bootstrap's Offset Classes Bootstrap 3 provides offset ...READ MORE

answered Mar 11 in Node-js by Tanvi
69 views
0 votes
1 answer
0 votes
1 answer

How to include a JavaScript file in another JavaScript file?

Let's assume you have two files: math.js: This ...READ MORE

answered Nov 27, 2024 in Java-Script by kavya
127 views
0 votes
1 answer

How do I delete a specific element from an array in JavaScript?

You can use the splice() method. Code: let arr ...READ MORE

answered Jan 10 in Java-Script by Navya
125 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP