Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get attributes values from select2 selected option

I'm using the Select2 plugin from http://ivaynberg.github.io/select2/select2-latest.html what works fine but i'm having a problem to get the values from the options attributes.

I've got a select menu like this:

<select name="invoice_line[0][vat]" class="vat">
    <option value="20440" data-value="21.00" data-name="20440" selected="selected">21%</option>
    <option value="20441" data-value="6.00" data-name="20441">6%</option>
    <option value="20442" data-value="0.00" data-name="20442">0%</option>
</select>

How can I get the values of the attributes 'data-value', 'data-name' and 'value' of the selected option?

like image 257
Leon van der Veen Avatar asked Oct 03 '13 09:10

Leon van der Veen


2 Answers

obj = $("#dropdown").select2("data")

in obj variable i will get all the data of the selected node.

like image 162
utkarsh Avatar answered Oct 13 '22 14:10

utkarsh


var return_option = $("#your-select-input-id").select2().find(":selected")[0];    

above given statement will return select option and then put that return result as given below:

var name_of_attribute = $( return_option ).attr('name-of-attribute');

This will return the attribute value.

like image 27
Hiren P Avatar answered Oct 13 '22 14:10

Hiren P