View Full Version : replace br tag with </div><div>, possible with mozilla?

06-08-2004, 08:06 PM
I find myself here yet again,

Again I am using an editable document in a web page. (document.body)

In IE using outerHTML you can convert a <br> inside a div to </div><div> thus creating two divs that give you the same effect as a <br>.

For example:

I want to change this:

<div>Here is some content. <br> Here is the second line.</div>

To this:

<div>Here is some content. </div><div> Here is the second line.</div>

Using mozilla 1.7 if you use the outerHTML the editor changes the "backwards " tag </div><div> to <div></div>, which obviously I do not want.

Heh, I tried making a text node and replacing the <br> but since it's a text node the html code is converted to html character entites.

I'm sure this one will keep me busy for a while. I know I could walk through the current node find all the nodes before the <br> and all the nodes after the <br>, delete the <br>'s parent, create two seprate div's for the content on either side of the <br> and finally insert those two divs.

Pretty ugly though, I was hoping someone would have a much better idea on how this could be done.

Thanks again for your time.

06-11-2004, 10:33 PM
This is a toughie, and it's a bit dangerous, too: it can yield an invalid document. Consider this starting point:
<p>Here is a line of text<br />
Here is the second line.</p>

By replacing the BR with /DIV DIV, you'll end up with this:
<p>Here is a line of text</div>
<div>Here is the second line.</p>

There's a lot of parent-element validation involved in this one.

BR isn't going to be deprecated any time soon, since the W3's HTML WG lost its backbone and caved with XHTML 2. It was to be replaced the <l> (L) element, for Line, but they've received a bunch of comments declaring that they're not the same thing, and that usually results in the inclusion of both. At least with the HTMLWG.

Whoa...sorry. Tangent over!

06-18-2004, 01:57 AM
Yes, I am aware of many situations like the one you explained. I acutally have it working just fine in IE . . . . lots of expection checking, heh.

I can't use <br> for various reasons I won't expound on.

Anyhow, a simple explanation how I got around around the evil <br>.

Grabing all the child nodes before it and all the child nodes after it. And then inserted the first div before the <br>'s parent and the replaced the <br>'s parent with the second div.

So it comes out like this:

<div>Here is some content. </div><div> Here is the second line.</div>

06-27-2004, 01:13 AM
document.body.innerHTML=document.body.innerHTML.replace(new RegExp("<br>","g"),'</div><div>');
Would this not work?