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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:35:08+00:00 2026-05-16T22:35:08+00:00

Supposing I have some random block of text in a single line. Like so

  • 0

Supposing I have some random block of text in a single line. Like so

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

But for whatever reason (width settings on the containing element, use of text-zoom etc.), on the viewer’s screen it displays as two or more lines.

Lorem ipsum dolor sit amet,

consectetur adipiscing elit.

or

Lorem ipsum dolor sit

amet, consectetur

adipiscing elit.

Is there any way to find out via javascript where those line-wraps happen?

$('p').text() and $('p').html() return Lorem ipsum dolor sit amet, consectetur adipiscing elit. regardless of how the text is displayed.

  • 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-16T22:35:09+00:00Added an answer on May 16, 2026 at 10:35 pm

    Here’s what I ended up using (feel free to critique and copy for your own nefarious purposes).

    First off, when the edit comes in from the user, it’s broken up with $(editableElement).lineText(userInput).

    jQuery.fn.lineText = function (userInput) {
       var a = userInput.replace(/\n/g, " \n<br/> ").split(" ");
       $.each(a, function(i, val) { 
          if(!val.match(/\n/) && val!="") a[i] = '<span class="word-measure">' + val + '</span>';
       });
       $(this).html(a.join(" "));
    };
    

    The newline replacement happens because the editing textbox is populated with $(editableElement).text(), which ignores <br/> tags, but they will still change the height of the following line in the display for typesetting purposes. This was not part of the initial objective, just fairly low-hanging fruit.

    When I need to pull out formatted text, I call $(editableElement).getLines(), where

    jQuery.fn.getLines = function (){
       var count = $(this).children(".word-measure").length;
       var lineAcc = [$(this).children(".word-measure:eq(0)").text()];
       var textAcc = [];
       for(var i=1; i<count; i++){
          var prevY = $(this).children(".word-measure:eq("+(i-1)+")").offset().top;
          if($(this).children(".word-measure:eq("+i+")").offset().top==prevY){
             lineAcc.push($(this).children(".word-measure:eq("+i+")").text());
       } else {
         textAcc.push({text: lineAcc.join(" "), top: prevY});
         lineAcc = [$(this).children(".word-measure:eq("+i+")").text()];
       }
       }
       textAcc.push({text: lineAcc.join(" "), top: $(this).children(".word-measure:last").offset().top});
       return textAcc;
    };
    

    The end result is a list of hashes, each one containing the content and vertical offset of a single line of text.

    [{"text":"Some dummy set to","top":363},
     {"text":"demonstrate...","top":382},
     {"text":"The output of this","top":420},
     {"text":"wrap-detector.","top":439}]
    

    If I just want unformatted text, $(editableElement).text() still returns

    "Some dummy set to demonstrate... The output of this wrap-detector."
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposing you have: <div><p>Some text</p><p>Some text</p></div> <div><p id='paragraph'>Some text</p></div> How can you use javascript
Consider you have some expression like i = j = 0 supposing this is
Supposing I have a StringerHelper(Component|Helper|Behaviour).php class with a method for random strings. I might
Supposing I have a 2 dimensional array which was created with something like this,
i have a table with 2 colums, id and date. supposing i have some
Lets say I have some simple classes for a twitter-like app: User Post A
Supposing I have 3 tables: Users, Roles and UserRoles. I can fetch the user_roles
Supposing I have one perl in /usr/bin (which came along with my os-distribution) and
Supposing I have this: foo bar 1 and foo bar 2 How can I
MVC newbie question re binders. Supposing I have two strongly typed partial actions that

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.