Gottfried Gottfried - 1 year ago 138
jQuery Question

How to use placeholder as default value in select2 framework

To get the chosen value of a

I'm using:

var x = $("#select").select2('data');
var select_choice = x.text

The problem is this throws an error if not value has been selected and I was wondering if there was any way to make it return the placeholder if no option is selected

Answer Source

You have to add as a first element to see a placeholder. From Select2 official documentation :

"Note that because browsers assume the first option element is selected in non-multi-value select boxes an empty first option element must be provided () for the placeholder to work."

Hope this help.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download