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

237
Views
Insert <br> tag to the second textbox JS

I'm building a simple text editor and I start with basics - as user press enter and start a new line a want to insert br to the the second textbox. The second textbox is meant to send ready formatted text to the database. As of now I managed to copy data from first to second textbox as user typing. Hence, I want to disable viewing br in the first textbox. Is there a way not to display br in the first textbox but technically have it. Thank you in advance!

DEMO: https://jsfiddle.net/mxr7hz3p/

Textboxes:

 <textarea type="text" id="first"></textarea>
 <textarea type="text" id="second"></textarea>

JS:

$(function (){
    $('#first').keyup(function (e){
        if(e.keyCode == 13){
            var curr = getCaret(this);
            var val = $(this).val();
            var end = val.length;
                
            $(this).val( val.substr(0, curr) + '<br>' + val.substr(curr, end));
    }})
});

    function getCaret(el) { 
       if (el.selectionStart) { 
          return el.selectionStart; 
       }
       else if (document.selection) { 
            el.focus(); 

            var r = document.selection.createRange(); 
                if (r == null) { 
                return 0; 
            } 

            var re = el.createTextRange(), 
            rc = re.duplicate(); 
            re.moveToBookmark(r.getBookmark()); 
            rc.setEndPoint('EndToStart', re); 

            return rc.text.length; 
         }  
         return 0; 
     }
$('#first').keyup(function(){
    $('#second').val(this.value);
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can simplify the JS in the fiddle to do this

$('#first').keyup(function(){
  var val = $(this).val();

  val = val.replace(/\n/g, '<br />')

  $('#second').val(val);
});

https://jsfiddle.net/t3vzgnp7/2/

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!