Prithviraj Mitra
Prithviraj Mitra

Reputation: 11812

Get select box name in jquery

I have couple of select box and I would like to know which select box is triggered.

Html code:

<form id="property-filters" method="post">
    <select name="sel1">
    <option value="1">1</option>
    <option value="2">2</option>
    </select>

    <select name="sel2">
    <option value="3">3</option>
    <option value="4">4</option>
    </select>
</form>

Jquery code:

$(document).on('change', '#property-filters input, #property-filters select', function () 
{
//Get the name of the select box.
}

Any help is highly appreciated. Thanks in advance.

Upvotes: 1

Views: 113

Answers (3)

Mudassir Hasan
Mudassir Hasan

Reputation: 28741

You can do

$('#property-filters select').change(function () {
   alert($(this).prop('name'));
});

Check JSFiddle

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115222

Check the name property of the event fired dom object.

$(document).on('change', '#property-filters input, #property-filters select',function () {
   if(this.name == 'sel1'){
       // do the rest
   }else if(this.name == 'sel2'){
       // do the rest    
   }
})

Upvotes: 2

Ultrazz008
Ultrazz008

Reputation: 1688

Use attr()

$(this).attr('name');

Upvotes: 1

Related Questions