hernansartorio / jquery-nice-select

A lightweight jQuery plugin that replaces native select elements with customizable dropdowns.
http://jqueryniceselect.hernansartorio.com
961 stars 285 forks source link

No way to get the selected value? #72

Open vohrahul opened 6 years ago

vohrahul commented 6 years ago

@hernansartorio

$('#original_select_id').val() doesn't work.

In the documentation, there's no way to get the selected value. Do I have to always traverse the DOM to get the data-value attribute of the list item?

Pre-added function would be such a great gift to this plugin.

Thanks

Kane-R-G commented 5 years ago

I managed to work out a way to get the selected value for anyone in the future:

You can add this else if statement to the Methods conditionals.

}else if (method == 'selected') {
          return $(this).next().find('.selected').text();
}else if (method == 'val') {
            return $(this).next().find('.selected').attr('data-value');

You can then call it like:

var n_select = $('.n-select').niceSelect();
n_select.niceSelect('selected'); //returns text
n_select.niceSelect('val'); //returns value

Which will return the text of the current selected option.

jonnzer commented 4 years ago

I think maybe u can try this .... $('#select1').next().find('li.selected').attr('data-value')

jonnzer commented 4 years ago

i just found... $('id').val() is ok ...