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

181
Views
Unable to update textarea value with formatted text

I am trying to write a JS function that takes a block of text pasted inside textarea, removes line breaks but keeps paragraph breaks, and updates the text inside that textarea before I submit a form.

I have attempted to do it this way:

<textarea name="sum" id="sum" onpaste="frmt()"></textarea>

JS:

function frmt() {
    const tar = document.getElementById("sum");
    const reg = /[\r\n](?![\r\n])/g;        
    const str = tar.value;  
    tar.innerHTML = str.replace(reg, "");
}

What am I missing?

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

0

<textarea name="sum" id="sum"></textarea>

<script>
  const sum = document.getElementById('sum');

  sum.addEventListener('paste', () =>
    setTimeout(
      () => (sum.value = sum.value.replace(/[\r\n](?![\r\n])/g, '')),
      0
    )
  );
</script>

https://developer.mozilla.org/en-US/docs/Web/API/Element/paste_event

about 4 years ago · Juan Pablo Isaza Report

0

<textarea> is not supported .innerHTML so you need to set values through .value and you can use oninput event.

This event occurs when the value of an <input> or <textarea> element is changed.

Helpful links:
https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/oninput https://www.w3schools.com/jsref/event_oninput.asp

function frmt(id){    
    const tar = document.getElementById(id);
    const reg = /[\r\n](?![\r\n])/g;
    const str = tar.value;
    tar.value = str.replace(reg, "")
}
textarea{
  width: 95%;
  height: 160px;
}
<textarea name="sum" id="sum" oninput="frmt('sum')"></textarea>

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!