Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    replace br tag with </div><div>, possible with mozilla?

    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:

    Code:
    <div>Here is some content. <br> Here is the second line.</div>
    To this:

    Code:
    <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.

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is a toughie, and it's a bit dangerous, too: it can yield an invalid document. Consider this starting point:
    Code:
    <div>
        <p>Here is a line of text<br />
            Here is the second line.</p>
    </div>
    By replacing the BR with /DIV DIV, you'll end up with this:
    Code:
    <div>
        <p>Here is a line of text</div>
        <div>Here is the second line.</p>
    </div>
    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!

  • #3
    New Coder
    Join Date
    May 2004
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    lots of expection checks

    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:

    Code:
    <div>Here is some content. </div><div> Here is the second line.</div>
    Last edited by iceboxqs; 06-18-2004 at 01:00 AM.

  • #4
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Code:
    document.body.innerHTML=document.body.innerHTML.replace(new RegExp("<br>","g"),'</div><div>');
    Would this not work?
    Shawn


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •