Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery add / remove multiple attribute on select

Tags:

jquery

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?

like image 964
John Avatar asked May 04 '11 09:05

John


People also ask

How to add multiple attributes 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, ...})

How to add or remove dropdown option from jQuery?

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.

How to remove append option in select in jQuery?

To remove all options from a select list using jQuery, the simplest way is to use the empty() method.


1 Answers

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

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

like image 77
Khoa Nguyen Avatar answered Sep 18 '22 02:09

Khoa Nguyen