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
    67
    Thanks
    19
    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. #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    4,836
    Thanks
    15
    Thanked 520 Times in 514 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. #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    9,244
    Thanks
    4
    Thanked 929 Times in 916 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. #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 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
  •