简体   繁体   中英

How can I send this to an onchange function for a select?

I have a select element and I want to set the onchange function and send 'this', I've tried these:

s.onchange= function() {console.log(this.selectedIndex);};

s.addEventListener('click', myFunction.bind(s), false);

But no luck, I'm still getting 'undefined'. What is this still showing undefined even if I am binding?

Im not sure why you'd want to send this over event , but the following should be close to what you're looking for?

var select = document.getElementById('select')

select.addEventListener('change', function(event) {
  readSelection(this)
})

function readSelection(input) {
  console.log(input)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM