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
    Dec 2011
    Posts
    27
    Thanks
    4
    Thanked 0 Times in 0 Posts

    div.innerHTML.replace("<p></p>",""); does not work

    I wanted to replace on a given webpage ALL empty paragraphs

    <p></p>

    or (mind the blank!)

    <p> </p>

    by nothing. Therefore I coded:

    var div = document.getElementById("foobar");
    div.innerHTML = div.innerHTML.replace("<p></p>","");

    ....but it doesn't work.

    What do I have to change?

    Furthermore I guess only the first occurence is changed. How do I replace ALL occurencies?

    Thank you
    Peter

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,135
    Thanks
    12
    Thanked 332 Times in 328 Posts
    How do I replace ALL occurencies?
    as the documentations state, using a RegExp with the g flag.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,479
    Thanks
    0
    Thanked 635 Times in 625 Posts
    div.innerHTML.replace(/<p><\/p>/g,"");
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    And if you want to consider white spaces inside the paragraph:
    Code:
    .replace(/<p>\s*<\/p>/g, "");


  •  

    Posting Permissions

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