user455318 user455318 - 1 month ago 13
jQuery Question

Remove text after element

I am trying to remove the text after the icon.

<i class="flagstrap-icon flagstrap-as" style="margin-right: 10px;">x</i>France

$('.flagstrap-icon').next().remove();


The problem is that next() will not work without an element.

Example
<span>France</span>


Demo

Any idea to remove the text after the icon?

Answer

jQuery can't really target nodes that aren't elements, but plain JS can

$('.flagstrap-icon').get(0).nextSibling.remove()

FIDDLE

Note that this uses the native remove(), which is not supported in older browsers, if you have to support those, you'd do

var node = $('.flagstrap-icon').get(0).nextSibling;
node.parentNode.removeChild(node);