4

As i have multiple drop-downs in my form, I would like to retrieve the HTML id from one of the selected drop-downs. I have the following code for my drop-downs on change:

$("select[name$='product_type']").change(function(){}

when using console.log($(this).select());

i can see the selected drop-down id in the console;

enter image description here

What is the syntax for retrieving this id into a var?

phicon
  • 3,313
  • 5
  • 29
  • 59

3 Answers3

4

Just use $(this).attr("id") to get the id.

You can also use this.id (as already mentioned in the comments). I just found a performance test for $(this).attr("id") vs. this.id with the result of this.id being faster, which is expected as it's pure javascript instead of a javascript library like jQuery.

matthias_h
  • 11,260
  • 9
  • 20
  • 39
1

You just take id property:

$("select[name$='product_type']").change(function() {
    console.log(this.id);
});
dfsq
  • 187,712
  • 24
  • 229
  • 250
-2

$("select[name$='product_type'] option:selected").attr("id"); inside the change callback.

ROMANIA_engineer
  • 51,252
  • 26
  • 196
  • 186
Avix
  • 1