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

207
Visualizações
How to use in Javascript the value of a variable obtained in a jQuery block

I've written a form that contains HTML select served by jQuery. The rest of HTML elements is served by pure Javascript. I want to get in Javascript part the value obtaied from jQuery block. Here is a simple code which covers the subject:

<!DOCTYPE html><html>
<head><meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
  $("#button").click(function() {
     var value = $("#city").val();
     alert(value);
  })
});
//how to get value from jQuery block above ? ?
var pure_value = value; 
. . . . . . . . . . . . //the next code using value 
</script>
</head>
<body>    Select City:
    <select id="city" name="city">
        <option value="delhi"> Delhi </option>
        <option value="mumbai"> Mumbai </option>
        <option value="chennai"> Chennai </option>
    </select>
    <button id="button"> Fetch Value </button>
</body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

var value will only have a city value when the user clicks the button, so it does not make sense to try to get the value from the variable declared inside button click callback.

You have some possibilities:

  1. Ask jquery for city val again in this line: var pure_value = $("#city").val();
  2. Create a function 'code_using_value(value)' and call this function inside the button callback.
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