2

If i have

<p>someword here</p>
<span>another thing here</span>
<div id="text">other thing here</div>
<!-- any other html tags -->

How do I insert a space in first and last position of the content?

I want the result to be

<p> someword here </p>
<span> another thing here </span>
<div id="text"> other thing here </div>
<!-- after tags like <p> and before </p> there have one space -->
yoozer8
  • 7,361
  • 7
  • 58
  • 93
cj333
  • 2,547
  • 20
  • 67
  • 110

5 Answers5

8

Naive (and incorrect!) example would be:

var victims = document.querySelectorAll('body *');
for( var i = 0; i < victims.length; i++ ) {
    victims[i].innerHTML = " " + victims[i].innerHTML + " ";
}

But once you run it, you will find out that all your elements got destroyed! Because, when you are changing innerHTML, you are changing element children as well. But we can avoid that, by not replacing content, but adding it:

var padLeft = document.createTextNode( " " );
var padRight = document.createTextNode( " " );
victims[i].appendChild( padRight );
victims[i].insertBefore( padLeft, victims[i].firstChild );

Looks cool! But, o no - we ruin our script tags, images and so on. Lets fix that too:

var victims = document.querySelectorAll('body *');
for( var i = 0; i < victims.length; i++ ) {
  if( victims[i].hasChildNodes ) {
    var padLeft = document.createTextNode( " " );
    var padRight = document.createTextNode( " " );
    victims[i].appendChild( padRight );
    victims[i].insertBefore( padLeft, victims[i].firstChild );
  }
}

Here you got the script :) Its not cross-browser all the way down to Netscape4, but is enough to understand basic idea.

Incognito
  • 20,537
  • 15
  • 80
  • 120
c69
  • 19,951
  • 7
  • 52
  • 82
2

If you insist using JS + RegExp to pad every element's innerHTML then you could do:

var 
    r = /(<[^>]+>)(.*)(<\/[^>]+>)/g,
    func = function(str) { 
        return str.replace(r, function(original, a, b, c) {
            return a + ' ' + (r.test(b) ? func(b) : b) + ' ' + c;
        });
    };

func("<p name='somename'>someword here</p>");
// "<p name='somename'> someword here </p>"

func("<div>I have things here<span>And more here<p>And even more here</p></span></div>");
// "<div> I have things here<span> And more here<p> And even more here </p> </span> </div>"

This is just to show how you could do this, but I highly recommend against it. The examples I provide is extremely simple. Anything like a normal page (say, the one you are looking at now) has all sorts of tags. This would be extremely exhaustive. And not very wise.

Marshall
  • 4,716
  • 1
  • 19
  • 14
  • 2
    Even with your recommendation against it, I feel it's important to re-enforce this is a very bad idea, and that using regular expressions to parse XML is just dumb. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Incognito Sep 21 '11 at 20:09
  • Yeah Im not sure why this is 1) an answer at all and 2) the accepted answer. Something more like `container.appendChild( document.createTextNode( '\u00A0' ) );` would be much more appropriate. (where `\u00A0` adds a non-breaking space)... but if that's what the OP wants, then fine. But, nobody else in the world should ever consider this a viable solution, ever. – dudewad Jun 15 '17 at 21:39
0

For a single element (as you seem to be asking):

element.html(' ' + element.html() + ' ')

For every element on the page (as your example seems to indicate), apply that to each element.

yoozer8
  • 7,361
  • 7
  • 58
  • 93
  • @harpo, thanks, is there a quickly way to use `Rex` because I want replace all the tags in the page. – cj333 Sep 21 '11 at 18:06
0

With jQuery*:

$('#id').html(' ' + $('#id').html() + ' ');

If you know that the elements do not have nested elements, it would be better to use the simpler:

$('#id').text(' ' + $('#id').text() + ' ');

(*) The reason for using jQuery and not plain javascript is that browsers (I'm looking at you, IE), have different inbuilt properties for getting and setting these values. jQuery saves you from having to worry about that.

harpo
  • 41,820
  • 13
  • 96
  • 131
  • @cj333, hmmm... modify every element in a live page using regex in javascript? You've driven me to wonder, why? – harpo Sep 21 '11 at 18:20
0

in your case:

$("*").html(function(index, html){ return " " + html + " "; });
roberkules
  • 6,557
  • 2
  • 44
  • 52