www.webdeveloper.com
Results 1 to 4 of 4

Thread: Simple forum script

  1. #1
    Join Date
    Jul 2011
    Posts
    3

    Simple forum script

    Hi,
    I have simple script to change the markups in the content of post. This is code with test:

    Code:
    <blockquote id="txt"><p>Test</p><p>Test</p></blockquote>
    
    <script type="text/javascript">
    var el = document.getElementById("txt").innerHTML;
    el = el.replace('</p><p>', '</p></blockquote><p>&nbsp;</p><blockquote><p>');
    document.getElementById("txt").innerHTML = el;
    </script>
    After launching it I should have output:

    Code:
    <blockquote id="txt">
    <p>Test</p>
    </blockquote>
    <p>&nbsp;</p>
    <blockquote><p>Test</p>
    </blockquote>
    But I have:
    Code:
    <blockquote id="txt">
    <p>Test</p>
    <p>&nbsp;</p>
    <blockquote><p>Test</p>
    </blockquote>
    </blockquote>
    What's wrong?

  2. #2
    Join Date
    May 2011
    Posts
    194

    replace string wrong ?

    Hi

    <blockquote id="txt"><p>Test</p><p>Test</p></blockquote>

    <script type="text/javascript">
    alert(document.getElementById("txt").innerHTML); // 1
    var el = document.getElementById("txt").innerHTML;
    var e2 = el.replace('</p><p>', '</p></blockquote><p>&nbsp;</p><blockquote><p>');
    document.getElementById("txt").innerHTML = e2;
    alert(document.getElementById("txt").innerHTML); // 2


    //Opera alert()
    // 1
    // <p>Test_</p><p>______________________________________Test</p>
    // 2
    // <p>Test_</p><p>_____________&nbsp;</p><blockquote><p>Test</p>
    // but you want
    // <p>Test_</p></blockquote><p>_&nbsp;</p><blockquote><p>Test</p>

    // replace string must be wrong ?
    // _ only filler to set positions
    </script>

  3. #3
    Join Date
    Jul 2011
    Posts
    3
    Code:
    <blockquote id="txt"><p>Test</p><p>Test</p></blockquote>
    
    <script type="text/javascript">
    alert(document.getElementById('txt').innerHTML); // 1
    var el = document.getElementById('txt').innerHTML;
    var e2 = el.replace('</p><p>', '</p></blockquote><p>&nbsp;</p><blockquote><p>');
    alert(e2); //2
    document.getElementById("txt").innerHTML = e2;
    alert(document.getElementById("txt").innerHTML); // 3
    </script>
    // 1
    <p>Test</p><p>Test</p>

    // 2
    <p>Test</p></blockquote><p>&nbsp;</p><blockquote><p>Test</p>

    // 3
    <p>Test</p><p>&nbsp;</p><blockquote><p>Test</p></blockquote>

    Replace string is correct. I test it on Firefox, maybe it's getElementById bug? How to fix it?

  4. #4
    Join Date
    Jul 2011
    Posts
    3
    Ok, I found solution:

    Code:
    <blockquote id="txt"><p>Test</p><p>Test</p></blockquote>
    
    <script type="text/javascript">
    var el = document.getElementById('txt').innerHTML;
    var index = el.indexOf('</p><p>');
    var afterSlice = el.slice(index);
    var beforeSlice = el.slice(0, index);
    afterSlice = afterSlice.replace('</p><p>', '<p>');
    
    var elToAdd = document.createElement('blockquote');
    elToAdd.innerHTML = afterSlice;
    
    document.getElementById('txt').innerHTML = beforeSlice;
    
    document.getElementById('txt').parentNode.insertBefore(elToAdd, document.getElementById('txt').nextSibling);
    
    </script>
    It works like a charm, thank you for help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles