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
  • 118,978
  • 58
  • 307
  • 400
ghost...
  • 975
  • 3
  • 16
  • 33

2 Answers2

14

Not elegant, but should work

target.innerHTML = target.innerHTML.replace(/\n|\t/g, ' ');
sabof
  • 8,062
  • 4
  • 28
  • 52
4

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

Simo Mafuxwana
  • 3,702
  • 6
  • 41
  • 59