pradeepchhetri
pradeepchhetri

Reputation: 2969

Change select tag attribute value

I have a drop down menu. By default it allows only one option to select among all. I want to create a checkbox which when checked should change that dropdown menu to allow multiple options to be selected. How can I achieve this?

<select id="test" name="host">
    <option value="host1">host1</option>
    <option value="host2">host2</option>
    .....
    .....
</select>  

I want this to be changed to following on checkbox being checked.

<select id="test" name="host" multiple="multiple">
    <option value="host1">host1</option>
    <option value="host2">host2</option>
    .....
    .....
</select>  

Upvotes: 0

Views: 362

Answers (1)

Rajat Singhal
Rajat Singhal

Reputation: 11264

You need to use javascript for that..use a library called 'JQuery', it makes it very simple..

Working Demo

$("#checkbox_id").change(function(){
    if($(this).is(':checked'))
       $("#test").attr('multiple', 'multiple');
});

Edit: for reverting back..

Working Demo

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

Upvotes: 3

Related Questions