Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to clean up HTML code?

Tags:

html

jquery

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

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

...and transform it into this:

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

If anyone can point me in the right direction, it would be greatly appreciated. Or if anyone knows of any libraries that can remove useless tags from HTML, that would be great too.


UPDATE

The above code is just an example. There could be 20 wrapped spans for example...

like image 331
Andrew Avatar asked Dec 16 '13 17:12

Andrew


People also ask

Does jQuery improve performance?

Minimizing direct DOM manipulation improves jQuery performance. Every time an element is created and inserted, time and capacity is needed. Using a cached selector with the append() method reduces the need for capacity. The following example shows how applying DOM control improves performance.

How can you remove and HTML element using jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.

Is jQuery used for DOM manipulation?

jQuery was created in 2006 by John Resig. It was designed to handle Browser Incompatibilities and to simplify HTML DOM Manipulation, Event Handling, Animations, and Ajax.

Do web developers still use jQuery?

Tons of web developers still use jQuery. Many still use it even for small-scale web app development, despite all of the hype for frameworks like React, Angular, etc.


1 Answers

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

$('span').unwrap();

FIDDLE

like image 54
adeneo Avatar answered Oct 25 '22 16:10

adeneo