user2787888
user2787888

Reputation: 23

Remove <b> tag from text

I want to make a bold text non-bold using Greasemonkey. I have only found ways to remove the tag along with its text, but not a way to simply remove the tag itself.

So how do I make

<b>
some text
</b>

just

some text

?

Upvotes: 2

Views: 1968

Answers (2)

w35l3y
w35l3y

Reputation: 8783

Assuming you've only one TextNode inside each B tag.

b.parentNode.replaceChild(b.firstChild, b);

Example : http://jsfiddle.net/DGTh5/

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1075219

Assuming you're dealing with elements, not a string:

function unwrapChildren(element) {
    var parent, node, nextNode;

    parent = element.parentNode;
    for (node = element.firstChild; node; node = nextNode) {
         nextNode = node.nextSibling;
         parent.insertBefore(node, element);
    }
    parent.removeChild(element);
}

Then call unwrapChildren with the b element. The above will move all of its child nodes (there's probably only one, a text node) into the b element's parent node where the b element is, and then remove the b element.

Upvotes: 1

Related Questions