user3709682
user3709682

Reputation:

Get DropDown value using its name in JQuery

I have these SELECTs in my page

 <select id="select_status_id44" name="select_status_id[44]" class="inputbox" id =      "status_id_44">
<option value="1">Pending</option>
<option value="2" selected="selected">Confirmed</option>
<option value="3">Cancelled</option>
<option value="4">Refunded</option>
<option value="5">Shipped</option>
<option value="6">Paid</option>
<option value="7">Complete</option>
</select>

And also this one

 <select id="select_status_id78" name="select_status_id[78]" class="inputbox" id =      "status_id_44">
<option value="1">Pending</option>
<option value="2" selected="selected">Confirmed</option>
<option value="3">Cancelled</option>
<option value="4">Refunded</option>
<option value="5">Shipped</option>
<option value="6">Paid</option>
<option value="7">Complete</option>
</select>

I want to do is that whenever any of these two SELECT is CHANGED, it shoul dget the select_status_id[ID] ID inside that tag...

For now I have simply done this but it does not work

$('select[name=select_status_id]').change(function() {    
    alert('dsada');
});

I know that I should not get using ID attribute because they are different for both ... but NAME attribute is same

Upvotes: 0

Views: 1638

Answers (3)

JayKandari
JayKandari

Reputation: 1248

You can try this on change for each select.

$("#select_status_id44").change(function(){
    var val = "select_status_id["+$(this).val()+"]";
    $(this).prop("name", val);
    console.log(val);
});

UPDATE: Check this Fiddle : http://jsfiddle.net/JayKandari/L8TWN/1

Upvotes: 2

Sridhar R
Sridhar R

Reputation: 20418

Try this way

$('select[name^="select_status_id"]').change(function() {    
    alert($(this).attr('name').split('[')[1].replace(']',''));
});

DEMO

Upvotes: 2

Bart Calixto
Bart Calixto

Reputation: 19705

if you can add a data-property to your select

<select data-id-value="42" ..... >

$('select').on('change', function() {
    console.log ( $(this).data('id-value') );
});

Upvotes: 0

Related Questions