I want to change whether a user can select only, one or multiple options in a select box based on a tick box above. i.e. if the tick box is ticked the user can select multiple values, if not ticked they can only select one value. What is the best way to do this using jquery?
jQuery | attr() Method The attr() method in jQuery is used to set or return the attributes and values of the selected elements. To set multiple attributes and values: $(selector). attr({attribute:value, attribute:value, ...})
The task is to remove the option elements from the select element using jQuery. Approach: Select the option from select which needs to remove. Use JQuery remove() method to remove the option from the HTML document.
To remove all options from a select list using jQuery, the simplest way is to use the empty() method.
$('#checkboxid').change(function(){
if ($(this).is(':checked')) {
$('#listbox').attr('multiple','multiple');
} else {
$('#listbox').removeAttr('multiple');
}
})
Demo: http://jsfiddle.net/ynhat/WCPue/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With