Multi select List with "Select All" option - Fast reply needed

mojo

Well-Known Member
Adept
Jun 9, 2011
642
80
67
Hello folks,

I need a multi select drop down with select all option in the top. If i select the select all, all the remaining fields should be selected. and then if i scroll down and click on any single one the previous selection should be gone and this newly clicked value should be selected.

Thanks in advance.
 

mojo

Well-Known Member
Adept
Jun 9, 2011
642
80
67
Thanks for the reply mate.
Those are for checkboxes. Anyways i've solved it my way by using a small jquery.
I had a small problem even before i posted this thread. The "var user = $('#users_list').val();" in the script.js file actually returns a Javascript array object. But i thought it was a string. That struck me up and made me post this thread. Anyway, thanks for the reply.

PHP:
//used in form
$list = array('all'=>'Select All','manoj'=>'Manoj','chakri'=>'Chakri','mojo'=>'Mojo');
$form['users_list']=array(
    '#id' => 'users_list',
    '#type'=>'select',
    '#title' => t('Users'),
    //'#default_value' => array('all', 'manoj', 'chakri', 'mojo'),
    '#options' => $list,
    '#multiple' => true,
    '#attributes'=>array('size'=>4),
);
 
 
//used in script.js
(function ($) {
    Drupal.behaviors.mymodule = {
        attach: function () {
            $(document).ready(function(){
                $("#users_list").click(function(){
                    if($(this).val() == 'all') {
                        $('#users_list option').attr('selected', 'selected');
                        var user = $('#users_list').val();
                        user.splice(0,1);
                        $('#users_list').val(user);
                    }
                });
            });
        }         
    };
}(jQuery))