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

196
Views
how to set the text of a pre tag using jquery

I'm using a pre tag to hold some raw formated text that has carriage returns. When the page is first displayed, it looks fine. Later, I want to refresh just the pre data. I've tried two ways to do this with jquery, one using .html() and the other way with .text(). Both sorta work, but the .html throws away the carriage returns and the .text double spaces the carriage returns! I've also tried .val() but that didn't work at all. Here's the code (of course I only use one of the jquery lines at a time.)

<pre id="QComment">Initial Text</pre>

at some time later,

$('#QComment').text(databack);   // or
$('#QComment').html(databack);
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

This is a common problem between *nix based systems and windows systems. Someone wrote a simple newline detection plugin for jQuery newlinecharacter

So, what you can do is:

$('#QComment').text(databack.replace(/\r\n/g,EOL));

Which, what you're doing is replacing all windows style line breaks with ones appropriate for the system viewing the data.

over 4 years ago · Santiago Trujillo Report

0

Works like a charm for me: fiddle demo

Maybe you have some encoding problem? An example vould help.

(.val() sets the value attribute on the pre tag, which of course has no effect.)

over 4 years ago · Santiago Trujillo Report

0

I would suggest not bothering to use jQuery in this instance, and just set the object using plain old javascript:

document.getElementById('QComment').innerHTML = yourHTML;

There's no reason to add the jQuery overhead to something as simple as this. If you want to use this on multiple elements, jQuery is fine:

$('#selector, .selector, [selector]').each(function(element, index){
  element.innerHTML = yourHTML;
});
over 4 years ago · Santiago Trujillo 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!