Question

I have a pretty bad formatted output after removing comment lines.

I'd like to remove the newlines that remained.

HTML

<html>




<div class="donottouch">
</div>


</html>

I'd like this to become

Wanted Output

<html>
<div class="donottouch">
</div>
</html>

Without entering the other divs.

Code I use to remove comment lines:

$('> *', $content).contents().each(function() {
    if (this.nodeType == 8) {
        $(this).remove()
    }
});

Thank you very much in advance!

Best regards,

Alex

Was it helpful?

Solution

You might want to take a look at jQuery.trim() utilitiy:

Description:

Remove the whitespace from the beginning and end of a string. The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.

Example:

Remove the white spaces at the start and at the end of the string:
$.trim(" hello, how are you? ");

Result:
"hello, how are you?"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top