Search code examples
javascriptjquerytwitter-bootstrapbootstrap-selectbootstrap-selectpicker

selectpicker : Get the data attribute of selected option


I'm having hard time in to getting the data attribute set to select option on bootstrap selectpicker .

I tried:

$('.selectpicker').on('changed.bs.select', function (e) {
    var selected = e.target.value;
    console.log("value :  ", selected ); // gives selected value
    console.log("data attribute:  ", $(e.target).data("price")); 
});

data attribute always returns undefined

what wrong I'm doing here ?


Solution

  • This works.

    $('.selectpicker').on("changed.bs.select", function() {
        var dataTypeAttribute = $('option:selected', this).attr("data-type");
    });