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

161
Visualizações
Why dont I get an alert when the text is added?

I know I can achieve this by adding the alert function in the button click listener body but I don't want to do it that way.I want whenever this textbox receives text, i get an alert.

this is the code

<!DOCTYPE html>
<html>
    <head>
 <script src="https://code.jquery.com/jquery-3.1.1.js"></script>
        <title>Page Title</title>
    </head>
    <body>
        <form>
<input id="check" type="text" name="check" >

        </form>

        <button id="test">ClickMe</button>

    
    
  <script>  
  $(document).ready(function() {
    
    $('#test').click(function () {
        $('#check').val('Example') ;

    })

        $('#check').on('input propertychange paste change', function () {
        
          alert("text added");


        })

    })
    </script>
    </body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Setting the value with Javascript doesn't trigger an event, so you need to trigger the event manually. Also, you really only need the "input" event. The others are irrelevant or redundant.

$('#check').trigger('input');

Or you can chain it since you're using jQuery:

$('#check').val('N001-01-1356/2017').trigger('input');

Example:

$(document).ready(function() {

  $('#test').click(function() {
    $('#check').val('N001-01-1356/2017').trigger('input');
  })

  $('#check').on('input', function() {
    alert("text added");
  })
  
})
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>

<form>
  <input id="check" type="text" name="check">
</form>

<button id="test">ClickMe</button>

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