Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery how to remove all <span> tags from a <div>?

I've tried both of the following but none of them worked:

$('#divhtml').remove('span')

$('#divhtml').find('span').remove()

EDIT: $('#divhtml').find('span').remove() worked on 2nd try.

like image 561
Laguna Avatar asked Jan 19 '12 19:01

Laguna


People also ask

How do I remove a span tag?

Inline text styles are often set by using the span tags. Activating this option will remove all span tags including their styles, classes etc.

Which methods are used for removing any element or content in jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.

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 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.


2 Answers

You have already used a correct statement:

$('#divhtml').find('span').remove()

This should work. Please provide more context... See this jsfiddle as "proof" that something else is wrong: http://jsfiddle.net/Pbgqy/

like image 130
Anders Arpi Avatar answered Sep 20 '22 15:09

Anders Arpi


Try this:

$("#divhtml span").remove()
like image 37
Troy Barlow Avatar answered Sep 18 '22 15:09

Troy Barlow