gauravK
gauravK

Reputation: 197

How to get addEventListener to work with a select tag

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.

Upvotes: 13

Views: 34425

Answers (1)

Rayon
Rayon

Reputation: 36599

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>

Upvotes: 29

Related Questions