Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a dropdown list is a multi-select

I have a generic dropdown list filler script which populates the select with the options returned from various jquery calls. It currently is intended for a single selection. I need to add to it the ability to fill a multi-select, which works as-is, but I don't want to include the initial * Please choose * option. I'm looking for either a jQuery or plain Javascript solution.

if (dropdown != null) {
    var regList = document.getElementById(dropdown);
    regList.options.length = 0;
    var opt = document.createElement("option");

    // ** if the dropdown is *not* a multiple="multiple", 
    // add the first option as an empty value

    opt.text = " -- Select --";
    opt.value = "";
    regList.options.add(opt);

    // **

    // loop thorugh child nodes and fill dropdown.
    $.each(arg.d, function () {
        opt = document.createElement("option");
        opt.text = this;
        opt.value = this;
        regList.options.add(opt);
    });
    $('.SpinImage').remove();
}
like image 897
InbetweenWeekends Avatar asked Oct 28 '15 15:10

InbetweenWeekends


People also ask

How do I know if a dropdown has multiple selections?

We can use any of the methods we used to select one value from the dropdown to select multiple values by invoking the methods multiple times for different values. The "Select " class provides a method, isMultiple(), using which we can first validate whether the dropdown allows us to select multiple values.

How do I know which dropdown is selected?

Use the tagName property to check if an element is a select dropdown, e.g. if (select. tagName === 'SELECT') {} .

Which attribute is used for multiple selection in drop down list?

The multiple attribute is a boolean attribute. When present, it specifies that multiple options can be selected at once. Selecting multiple options vary in different operating systems and browsers: For windows: Hold down the control (ctrl) button to select multiple options.


1 Answers

You can check the .multiple property of the object

var isMulti = document.getElementById('selectId').multiple; // true/false

jQuery you could do .prop('multiple');

$('#selectId').prop('multiple');

http://jsfiddle.net/31eLyzb3/4/

/*
    Using jQuery, finds and populates the selects on the page.
*/
(function populateAll() {
    /* Here's our options info */
    var opts = {
        'volvo': 'Volvo',
        'saab' : 'Saab',
        'opel' : 'Opel',
        'audi' : 'Audi'
    };
    /*
        This function, given a jQuery select item, and options list, populates it.
        If it is a single-select, it adds a ---Select---/empty value as the first option.
    */
    function populate(listBox, options) {
        var option = null;
        if (listBox.prop('multiple') === false) {
            option = document.createElement('option');
            option.text = '---Select---';
            option.value = '';
            listBox.append(option);
        }
        $.each(options, function (value, label) {
            option = document.createElement('option');
            option.text = label;
            option.value = value;
            listBox.append(option);
        });
    }
    /* Find all select tags on page and populate with 'opts' data */
    $('select').each(function() {
        populate($(this), opts);
    });
})();
like image 180
smcd Avatar answered Oct 31 '22 20:10

smcd