Would it be possible to find and untag spans that do not have ids within a string? I have a text that has bunch of spans some of which have ids and others don’t.
Input:
<span>Hi there!</span><span id="blabla">This is a test</span>
Output:
Hi there!<span id="blabla">This is a test</span>
I prefer JavaScript functions but I wouldn’t mind using jQuery if it makes things easier!
You should be able to use a combination of the
:notpseudo-selector, and a “has-attribute” selector:Here’s a working example. Notice how the HTML code is made up of 4
spanelements, the CSS rule applies to allspanelements, but does not apply to the 2spanelements without anid, because they have been unwrapped by the above jQuery.The
contentsmethod returns all of the children of the selected elements, andunwrapremoves the parent, which in this case will be the unwantedspan.