7

I have a dom node in variable and i want to remove all enter/line break, tabs between the html tags. Basically i want to minify it without using external library. How can i do it.

var target = document.getElementById('myid');
var wrap = document.createElement('div');
wrap.appendChild(target.cloneNode(true));

wrap contains the node..

vsync
  • 103,437
  • 51
  • 275
  • 359
ghost...
  • 975
  • 2
  • 15
  • 32

2 Answers2

13

Not elegant, but should work

target.innerHTML = target.innerHTML.replace(/\n|\t/g, ' ');
sabof
  • 7,922
  • 3
  • 27
  • 51
4

You could replace the line breaks with an empty string target.replace(/(\r\n|\n|\r)/gm,"");

Simo D'lo Mafuxwana
  • 3,552
  • 6
  • 40
  • 55