Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
jQuery.text() retrieves all spaces and line returns from HTML element

I have some text displayed in a HTML table cell. The text includes some spaces and other line returns that are not visible when I look at the HTML output on my screen (just like in the snippet below).

Using jQuery.text() method, all these spaces and line returns are copied along with the text.

What should I use to grab what is visually displayed by my browser (as shown in the Expected output textarea)?

$(function() {
  let str = $('#address').text();
  $('textarea.paste').val(str);
});
div {
  display: inline-block;
  margin-top: 10px;
}
<table>
  <tbody>
    <tr>
      <td id="address">
        Some word<br>

        Some
        more
        words
      </td>
    </tr>
  </tbody>
</table>

<div>
  <code>jQuery.text()</code> output:<br>
  <textarea class="paste" rows="7"></textarea>
</div>
<div>
  Expected output:<br>
  <textarea rows="7">Some word&#13;Some more words</textarea>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can read the innerText property:

$(function() {
  let str = $('#address').prop('innerText');
  $('textarea.paste').val(str);
});
div {
  display: inline-block;
  margin-top: 10px;
}
<table>
  <tbody>
    <tr>
      <td id="address">
        Some word<br>

        Some
        more
        words
      </td>
    </tr>
  </tbody>
</table>

<div>
  <code>innerText</code> output:<br>
  <textarea class="paste" rows="7"></textarea>
</div>
<div>
  Expected output:<br>
  <textarea rows="7">Some word&#13;Some more words</textarea>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!