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

212
Visualizações
Input box alerts the text by clicking a button (Really new to JQuery)

JQuery Part

I just started using JQuery. First I wanted to create a a input box with an button that alerts the text that is in that box. This is the way I wanted to do that.

$(document).ready(function() { 
  $('#hit').click(function() { 
    alert($('#term').val());
  });
});

That is the code in the html body part. Here im not sure if I have to declare the id with an # or not. I tried both but It didnt work for both.

My Code

<!DOCTYPE html> 
<html>
   <head>
      <title>Button Event</title>
      <script src="cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> $(document).ready(function() { $('#hit').click(function() { alert($('#term').val()); }); }); </script> 
   </head>
   <body> <input id="term" type="text" value="enter your search"> <button id="hit" type="button" name="Button">Search</button> </body>
</html>

I am really new to JavaScript and Jquery. When I run this code there are no errors or something like that in the console just blank. The Button and Input field are on the website but when I enter something and click the button after that nothing happens. That´s why im sure there are some mistakes in that code.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

$(document).ready(function() { 
  $('#hit').click(function() { 
    alert($('#term').val());
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="term" type="text" value="enter your search" />
    <button id="hit" type="button" name="Button">Search</button>

here has worked, do you have imported a jquery library?

Resolution with your code:

<!DOCTYPE html> 
<html>
   <head>
      <title>Button Event</title>
   </head>
   <body> <input id="term" type="text" value="enter your search"> <button id="hit" type="button" name="Button">Search</button>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script>
         $(document).ready(function() { $('#hit').click(function() { alert($('#term').val()); }); });
      </script>
   </body>
</html>

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