Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7399913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:07:23+00:00 2026-05-29T04:07:23+00:00

Consider such html piece: <p>foo</p><p>bar</p> If you run (for example) jQuery text for it

  • 0

Consider such html piece:

<p>foo</p><p>bar</p>

If you run (for example) jQuery text for it you will get “foobar” — so it is raw text actually, not textual representation.

I am looking for some ready to use library to get textual representation, in this case it should be — “foo\nbar”. Or clever hints how to make this as easy as possible ;-).

NOTE: I am not looking for beautiful output text, but just preserved meaning of whitespaces, so for:

<tr><td>foo</td><td>bar</td></tr>
<tr><td>1</td><td>2</td></tr>

I will be happy with

foo bar
1 2

it does NOT have to be:

foo bar
1   2

(but of course no harm done).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:07:24+00:00Added an answer on May 29, 2026 at 4:07 am

    Have you looked at the innerText or textContent properties?

    function getText(element){
        var s = "";
        if(element.innerText){
            s = element.innerText;
        }else if(element.textContent){
            s = element.textContent;
        }
        return s;
    }
    

    Example

    Adds a PRE tag to the body and appends the body text.

    document.body.appendChild(
        document.createElement('pre')
    )
    .appendChild(
        document.createTextNode(
            getText(document.body)
        )
    );
    

    Edit

    Does using a range work with firefox?

    var r = document.createRange();
    r.selectNode(document.body);
    console.log(r.toString());
    

    Edit

    It looks like you’re stuck with a parsing function like this then.

    var parse = function(element){
        var s = "";
        for(var i = 0; i < element.childNodes.length; i++){
            if(/^(iframe|noscript|script|style)$/i.test(element.childNodes[i].nodeName)){
                continue;
            }else if(/^(tr|br|p|hr)$/i.test(element.childNodes[i].nodeName)){
                s+='\n';
            }else if(/^(td|th)$/.test(element.childNodes[i].nodeName)){
                s+='\t';
            }
    
            if(element.childNodes[i].nodeType == 3){
                s+=element.childNodes[i].nodeValue.replace(/[\r\n]+/, "");
            }else{
                s+=parse(element.childNodes[i]);
            }
        }
        return s;
    }
    
    console.log(parse(document.body)); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider such code (this is just example not real code): class Foo(url : String)
Consider the following HTML tables: <table id=myTable1> <tr><td><input type=text id=quantity1 name=quantity1 /></td></tr> <tr><td><input type=text
If we consider Man In the Middle Attack; Can such an attack occur if
Consider these two function definitions: void foo() { } void foo(void) { } Is
Consider the Oracle emp table. I'd like to get the employees with the top
Consider the two following regular expression snippets and dummy HTML that it should be
Let's consider that I want to extend an existing JSF component, such as the
Remark: please consider XPath syntax dead here, thank you. I have xml node (HTML
I'm using pQuery (a Perl port of jQuery) to select elements and retrieve text
Consider a product where changes a client is making to a text file are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.