Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native JavaScript select onchange this.value undefined

I have a select tag that is generated by a php file that I would rather not have to change if it can be avoided. I also do not have the ability to use jQuery to solve my problem. So here is how I am trying to set an onchange event:

var d = document.getElementById('lang_choice');
d.onchange = function(){
    window.alert(this.value);
}

The pop up box just says undefined. I have checked the html and it has the value attributes set in the option tags. So I am guessing I misunderstand something about this system and some explanation would be great.


1 Answers

var d = document.getElementById('lang_choice');
d.onchange = function(){
    window.alert(this.options[this.selectedIndex].value);
}

It works in older browser.

Although your method should work. There must be error elsewhere in your code. Check console.

like image 106
rgtk Avatar answered Mar 21 '26 21:03

rgtk