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

336
Views
how to get value of selected datalist by javascript on change

I am getting an alert called while refreshing the page but I want to get the value of the datalist by selection of country. can I get value by onchange?

function fun(el) {
  var x = document.getElementById('countrydata').value;
  alert(x)
}
<datalist id="countrydata" onchange="fun(this);">
  <option value="Afghanistan">Afghanistan</option>
  <option value="Åland Islands">Åland Islands</option>
  <option value="Albania">Albania</option>
  <option value="India">India</option>
</datalist>
<label for="country">country</label>
<input type="text" list="countrydata" id="country" name="country" size="50" autocomplete="off" />

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Move the onchange="fun(this);" from datalist to input.

<datalist id="countrydata">
     <option value="Afghanistan">Afghanistan</option>
     <option value="Åland Islands">Åland Islands</option>
     <option value="Albania">Albania</option>
     <option value="India">India</option>
   </datalist>
<label for="country">country</label>
<input type="text"
  list="countrydata"
  id="country" name="country"
  size="50"
  autocomplete="off"
 onchange="fun(this);" />
<script>
    function fun(el){
        var x = document.getElementById('country').value;
        alert(x)
    }
 </script>
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!