We help IT Professionals succeed at work.
Get Started

jQuery body text replacement

CHINOS asked
Last Modified: 2012-05-09

I need to capture all of the text of the body and replace it after I have ran it against a reg expression.  

I have tried the following ways already (these are without the expression applied, just trying for basic html replacement here)....

Method 1
    var html = $('body').html();

### the problem with this method is it doens't return the <body> tags when it replaces it since the jQuery replaceWith function actually deletes the code it finds, I've also tried manually inserting the tags into the parameters of the function but it doenst work.  

The way to fix it would seem something like this,
but that will fail because replaceWith is not a function of html.

Method 2
    var html = $('body').parents().html();

### the problem with this method is it includes everything in the head also, so obviously it fails when it tried to include the head twice.  

Method 3
    $('body').children().each(function() {

### this doesnt work either, here I'm trying to find the html by going from the child node...

My body code is here...

    <h1>my header</h1>
    <div id="wrapper">
    <div class="col1">
        <div id="ref">
            <p>some text</p>

Any ideas?  
Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE