Skip to content Skip to sidebar Skip to footer

Using JQuery To Clean Up HTML Code?

Using jQuery, does anyone know how I can clean this markup:

Solution 1:

Just unwrap all the span tags, that would leave only the inner span tag.

$('span').unwrap();

FIDDLE


Solution 2:

You could also do this:

$('span').each(function() {
    if($(this).prop('tagName') == $(this).children(":first").prop('tagName')) {
       $(this).children(":first").unwrap();
    }
});

Solution 3:

It is not a jQuery solution, but I don't know if you need it to live process your HTML. Otherwise you could use this online tool: http://www.dirtymarkup.com/ it will transform your code:

<span style="font-size:19px">
  <span style="font-size:20px">
    <span style="font-size:21px">
      Something
    </span>
  </span>
</span>

into:

<span style="font-size: 19px">Something</span>

Post a Comment for "Using JQuery To Clean Up HTML Code?"