Skip to content Skip to sidebar Skip to footer

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'v

Solution 1:

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

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

Solution 2:

This was answered in another SO question

There is no direct method with select2, you can use a combinaison of select2 data and jQuery :

$("#example").select2().find(":selected").data("id");

First you get the select2 data then you find your selected option with jQuery and finally the data-attribute.

Solution 3:

We can use a combination of select2 data and jQuery :

$("#example").select2('data').element[0].attributes['data-name'].value

Solution 4:

Following worked for me. Idea is to use "change" function as follows

<select class="drop-down">
     <optionvalue="0">A</option><optionvalue="1">B</option>
</select>

$('.drop-down').select2().on("change", function(e) {
    var obj = $(".drop-down").select2("data");
    console.log("change val=" + obj[0].id);  // 0 or 1 on change
});

Solution 5:

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.

Post a Comment for "Get Attributes Values From Select2 Selected Option"