2

How to remove child element and spaces using jquery?

I have the following content:

<div class="sectionA"> <p>sfadfafdafdafdaf</p>  </div>

If I do:

$(".sectionA *").remove();

I get this:

<div class="sectionA">   </div>

How do I remove the spaces after removing the <p> child element?

2
  • 1
    try $(".sectionA").empty(); Commented Jan 24, 2013 at 21:01
  • 1
    Here is a great script I have used before: stackoverflow.com/questions/1539367/… Commented Jan 24, 2013 at 21:02

2 Answers 2

4

What you need is the .empty() method, which does just what you described:

This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element is considered a child node of that element.

Example:

 $(".sectionA").empty();

jsFiddle Demo

Sign up to request clarification or add additional context in comments.

Comments

2

Place an empty string inside it:

 $(".sectionA").html('');

1 Comment

agreed, since you are wanting the <p> tags removed as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.