I'm trying to remove white space between tags so that childNodes only contain those tags nodes not the white space nodes too. Here's my code :
<li>
<label for="firstName" class="mainLabel">First Name : </label>
<input type="text" name="firstName" id="firstName"/>
<span>This must be filled</span>
</li>
And here's the JS code :
var parentHTML = firstName.parentNode.innerHTML;
parentHTML = parentHTML.replace(/>\n</g,"><");
firstName.parentNode.innerHTML = parentHTML;
But when i alert parentHTML
i get the same old string.
It's (not, see after the rule) because strings are immutable, I think, and you're setting the innerHTML of the parent element to be the exact same string you retrieved from it earlier.
Instead, I'd suggest:
JS Fiddle demo.
With regards to the comment from jfriend00 (below), it seems the regular expression was the problem, the
\n
didn't match the supplied pattern, that being the case, the following amendment satisfies teh requirements:JS Fiddle demo.
References:
Can you treat a html tag as a string in js? I guess it can be done. try this!
For most cases, I recommend removing space from:
>
character<
characterThere are two cases I can think of where this will not do what you want, and these are the same two cases that impact the less aggressive solutions above.
Empty space between
inline-block
elements is actually an intended or expected part of the layout. If this space is collapsed to zero characters, the implicit space between elements is removed. This can be avoided by changing my regex below to replace with a" "
.My original answer has been updated to preserve whitespace in
<script>
,<style>
,<pre>
, or<textarea>
tags. All of these except<pre>
are CDATA which means the content is not HTML and are parsed until the closing tag is found, which means the regex is a complete solution. If a<pre>
is nested or thewhite-space
CSS property is used, this will not preserve your content.The solution:
only spaces:
new line, tabs and spaces:
https://regex101.com/r/sD7cT8/1
I came across this thread because I was searching for a solution to eliminate gaps around divs caused by white space in HTML source, or line feeds in my case.
Before I realized that white space could cause these gaps, I was going nuts trying to get rid of them. I want to keep my HTML source formatted for readability, so compressing the code is not a good solution for me. Even if I handled it this way, it doesn't fix divs that are generated by Google and other vendors.
I started by creating the following function and calling it in body onload.
This seemed to work perfectly, but unfortunately, it breaks the Google search box I have in my footer.
After trying many variations of the regex pattern without success, I found this regex tester at http://www.regexpal.com/. I far as I can tell, the following pattern does what I need.
That said, the function was still breaking the search box. So I ended up moving it into a jQuery document ready function. Now it's working and does not break the search box.