I have some simple HTML which I need to strip simple formatting.
A nice house was found in <b>Toronto</b>.
I need to remove the bold, but leave the sentence intact.
How is this possible in jQuery?
I have some simple HTML which I need to strip simple formatting.
A nice house was found in <b>Toronto</b>.
I need to remove the bold, but leave the sentence intact.
How is this possible in jQuery?
You can also use
.replaceWith()
, like this:Or if you know it's just a string:
This can make a big difference if you're unwrapping a lot of elements since either approach above is significantly faster than the cost of
.unwrap()
.Another native solution (in coffee):
I don't know if it's faster than user113716's solution, but it might be easier to understand for some.
This selects all
<b>
elements, then uses.contents()
to target the text content of the<b>
, then.unwrap()
to remove its parent<b>
element.For the greatest performance, always go native:
This will be much faster than any jQuery solution provided here.
The simplest way to remove inner html elements and return only text would the JQuery .text() function.
Example:
jsFiddle Demo
How about this?
The first line copies the HTML contents of the
b
tag to the location directly after theb
tag, and then the second line removes theb
tag from the DOM, leaving only its copied contents.I normally wrap this into a function to make it easier to use:
All of the code is actually pure Javascript, the only JQuery being used is that to select the element to target (the
b
tag in the first example). The function is just pure JS :DAlso look at: