Get id from select box

javascript, jquery

Solution

Use `this` keyword and change your selector to `$('#form select')`

Demo

$(document).ready(function() {
  $('#form select').change(function() {
    var strChosen = $(this).attr('id');
    alert(strChosen);
  });
});

Note: You can also use `.on()` method for `change` event like

$('#form select').on('change',function() {});

For more information on the syntax, you can refer @sbaaaang's answer

Problem

I have multiple select boxes whithin a form like this: ``` <form action="foo" method="post" id="form"> <select id="one"> <option value="foo1"></option> <option value="foo2"></option> <option value="foo3"></option> </select> <select id="two"> <option value="bar1"></option> <option value="bar2"></option> <option value="bar3"></option> </select> <select id="three"> <option value="baz1"></option> <option value="baz2"></option> <option value="baz3"></option> </select> </form> ``` Now, if any of these three boxes change, I wanna know which one of them. I tried it like the following, but I only get the id of the first box. Do I have to write it for each select or is there a way to get the id of the changed select box? ``` $(document).ready(function() { $('#form').change(function() { var strChosen = $('select').attr('id'); alert(strChosen); }); }); ```

Original source