Search code examples
javascriptjqueryregexinnerhtmltrim

Jquery - trim inner HTML?


I somehow need to trim() the innerHTML of my content... so I have something like this:

<div>
     <b>test</b>

123 lol
          </div>

I basically want to rid of the white space that is ONLY between <div> and the next character, and the white space just before the closing </div>.

So the outcome would be:

<div><b>test</b>

123 lol</div>

Solution

  • var $mydiv = $('#mydiv');
    $mydiv.html($.trim($mydiv.html());
    

    This should take the contents any element, trim the whitespace from it and reset it as the content.