Jquery adds / removes multiple attributes when selected

I want to change whether the user can select only one or several options in the selection field, based on the checkmark above. that is, if the checkmark is ticked, the user can select several values, if they are not checked, they can select only one value. What is the best way to do this using jquery?

+5
source share
3 answers
$("#theCheckbox").change(function() {
    $("#theSelect").attr("multiple", (this.checked) ? "multiple" : "");
}).change();

You can try it here.

+3
source

For a newer jQuery setup customization instead of attr works:

http://jsfiddle.net/DdhSF/267/

$("#theCheckbox").change(function() {
    $("#theSelect").prop("multiple", (this.checked) ? "multiple" : "");
}).change();
+5
source
$('#checkboxid').change(function(){

    if ($(this).is(':checked'))
    {
        $('#listbox').attr('multiple','multiple');
    }
    else
    {
        $('#listbox').removeAttr('multiple');
    }
})

Demo: http://jsfiddle.net/ynhat/WCPue/1/

+2
source

All Articles