Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get addEventListener to work with a select tag

Tags:

javascript

HTML

<select id="country" >
 <option value=""></option>
 <option value="india">India</option>
 <option value="australia">Australia</option>
 <option value="turkey">Turkey</option>
</select> 

JS

var a = document.getElementById('country');
b.addEventListener('select',function () {alert(this.value);} ,false); 

I want to show the selected value in an alert box when the user selects an option from the list.

like image 747
gauravK Avatar asked May 31 '16 06:05

gauravK


1 Answers

Use change event as select event is invoked when some text is selected in an element.

var a = document.getElementById('country');
a.addEventListener('change', function() {
  alert(this.value);
}, false);
<select id="country">
  <option value="" disabled selected>Please select your country</option>
  <option value="India">India</option>
  <option value="Australia">Australia</option>
  <option value="Turkey">Turkey</option>
</select>
like image 152
Rayon Avatar answered Nov 09 '22 01:11

Rayon