Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove children in jQuery?

Tags:

jquery

In the following example

http://jsfiddle.net/pDsGF/

I want to remove just class 'child' from class 'parent'. I have tried

.remove($('parent').children('child'))

But it doesn't work

like image 695
Hoa Avatar asked Jul 29 '12 21:07

Hoa


People also ask

Will remove all child nodes of the set of matched elements from the DOM in jQuery?

The empty() method removes all child nodes from the set of matched elements.

How add and remove in jQuery?

Given an HTML element and the task is to add and remove multiple classes from it using JQuery. Approach: First select the element to which multiple classes will be added. Then use addClass() method to add multiple classes to the element and removeClass() method to remove multiple classes.

What is remove in jQuery?

jQuery remove() Method The remove() method removes the selected elements, including all text and child nodes. This method also removes data and events of the selected elements. Tip: To remove the elements without removing data and events, use the detach() method instead.

How do I remove all elements from a div?

The empty() method removes all child nodes and content from the selected elements.


2 Answers

You need periods to get elements by class, for one. For two, that syntax isn't correct.

$('.parent .child').remove();

Here's a demo.

like image 165
Ry- Avatar answered Oct 04 '22 18:10

Ry-


Do you want to remove the childs (with a class "child") of parents (with a class "parent")?

$('.parent').children('.child').remove();

Or simply:

$('.parent .child')​.remove()​;
like image 35
Besnik Avatar answered Oct 04 '22 19:10

Besnik