Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda