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,549
  • 5
  • 32
  • 62

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,356
  • 9
  • 22
  • 40
1

You just take id property:

$("select[name$='product_type']").change(function() {
    console.log(this.id);
});
dfsq
  • 191,768
  • 25
  • 236
  • 258
-2

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

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Avix
  • 1