Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!