Replace whitespaces to " "

I want to replace all whitespaces to  

<div class="red-c"><p>Hel  lo <span style="background-color: rgb(204, 193, 217);">h   el  lo</span> <span style="font-size: 18px;">h<span style="background-color: rgb(127, 127, 127);">e  ll</span>o</span> h   ello
</p></div>

with the result

<div class="red-c"><p>Hel&nbsp;&nbsp;lo&nbsp;<span style="background-color: rgb(204, 193, 217);">h&nbsp;&nbsp;&nbsp;el&nbsp;&nbsp;lo</span>&nbsp;<span style="font-size: 18px;">h<span style="background-color: rgb(127, 127, 127);">e&nbsp;&nbsp;ll</span>o</span>&nbsp;h&nbsp;&nbsp;&nbsp;ello
</p></div>

I’ve tried

$(".red-c").html().split(" ").join("&nbsp;"))

but this also replaces the white spaces in the html tags.

Use text() instead of html() ?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.