I am trying to alter the HTML output of a widget I have no control over. Basically the issue is that the widget outputs HTML that uses divs inside of a p which is not valid..
<div class="widget_output">
<ul>
<li>
<p>
<div>This is a random item</div>
<div>This is a random item</div>
<div>This is a random item</div>
<div>This is a random item</div>
</p>
</li>
<li>
<p>
<div>This is a random item</div>
<div>This is a random item</div>
<div>This is a random item</div>
<div>This is a random item</div>
</p>
</li>
</div>
My idea is to change the divs into spans which are inline elements, the HTML should then be valid. I have been looking at jquery to try and select the class and then convert these divs into spans. Can anyone point me in the right direction or a tutorial of someone achieving something similar?
The phrasing elements can only contain other phrasing elements, for example, you can't put div inside span.
Divs and spans are not interchangeable in web page building.
The difference between span and div is that a span element is in-line and usually used for a small chunk of in-line HTML whereas a div (division) element is block-line (which is basically equivalent to having a line-break before and after it) and used to group larger chunks of code. Save this answer.
Just stuck a JSFiddle together:
http://jsfiddle.net/daYL4/3/
$(".widget_output div").replaceWith(function() {
return "<span>" + this.innerHTML + "</span>";
});
Seems to work better than my original suggestion. But take a look at the answers to this related question, as they cover some good points:
Use jQuery to change an HTML tag?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With