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

134
Visualizações
Show input values in a p tag

I have an input element, what I want to achieve is whenever I type into the input, the characters will be inserted into a p tag and will be displayed. The code I have below partially works. The problem I have with my code is the characters will start to show in the p tag when I type the second character. I want it to start showing after 1st typed character. How can I achieve this?

$('input').keypress(function() {
  if ($(this).val().length >= 0) {
    $('p').html($(this).val())
  }
});

$('input').keyup(function(e) {
  if (e.keyCode == 8) {
    if ($(this).val() == "" || $(this).val() == null) {
      $('p').html('')
    }
    $('p').html($(this).val())
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input />
<p></p>

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

I think input event is ideal for this case. Also catches copy-paste into the field.

$('input').on('input', function() {
  $('p').html($(this).val())
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input />
<p></p>

about 4 years ago · Santiago Gelvez Relatório

0

You just use keyup function.

$('input').keyup(function() {
  if ($(this).val().length >= 0) {
    $('p').html($(this).val())
  }
});

$('input').keydown(function(e) {
  if (e.keyCode == 8) {
    if ($(this).val() == "" || $(this).val() == null) {
      $('p').html('')
    }
    $('p').html($(this).val())
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input />
<p></p>

about 4 years ago · Santiago Gelvez 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