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 8836631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:32:23+00:00 2026-06-14T09:32:23+00:00

How could I get the text of the body that is before a specific

  • 0

How could I get the text of the body that is before a specific element using Javascript/jQuery?
What I want to achieve is finding out the position of an element respect of the body, for instance, if the body text is 100 characters long and the text before the node is 50, that node would be at 50%.
Getting body’s length is easy, but getting the text before an element is not as trivial as I’d thought in first instance.

EDIT:

To make it clear, I’ll give an example:

<body>
   <p>
      ...some text <span>that could have sub-elements</span>
   </p>
   <p>
      ...some text
      <ul>
         <li>now with a list</li>
         <li>why not?</li>
      </ul>
   </p>
   <div>
      <p>...some text...</p>
      <div>
          <p>...some text...</p>
          <p id="this_is_the_element">what's there in the body before this element?</p>
          <p>...some text...</p>
          <p>...some text...</p>
      </div
   </div>
</body>

In this case, the specific node would be #this_is_the_element and what I would expect to get is something like:

...some text that could have sub-elements 
...some text 
now with a list 
why not? 
...some text 
...some text
  • 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-06-14T09:32:24+00:00Added an answer on June 14, 2026 at 9:32 am

    I can propose this function :

    ​function textUntil($element) {
        var T = '#~~~#';
        var $marquer = $('<span>'+T+'</span>');
        $marquer.insertBefore($element);
        var text = $(document.body).text().split(T)[0];
        $marquer.remove();
        return text;
    }
    

    The trick is to insert a unique marker and get the text until this marker. Of course the inserted element is removed before the HTML is rendered. It will work for any kind of body.

    Demonstration (open the console)

    EDIT :

    Demonstration with the HTML of your edit (open the console)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I could get the handle to the google text doc i needed. I am
Was hoping I could get a bit of a hand on this login that
I was under the impression that I could get the value of a select
I could reproduce my bug using servside OAuth2.0 only so it's not javascript and
I am using Selenium-WebDriver for Ruby and I am trying to verify that text
Problem Scripts on some pages that use jquery are not activating. Scenario Finding that
I want to create a PHP program that could create a .php file that
I'm trying to use the FitText jquery plugin to get resize some text. The
Is there a way to get the body text of a notification to scroll
I could get the keyboard to work but on a UITextView but some how

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.