Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multi-select box without ctrl-click

I am using the code which is written in this stackoverflow post

How to avoid the need for ctrl-click in a multi-select box using Javascript?

it is recommended also in many other articles, to do multiple selection without ctrl-click.

the code:

$('option').mousedown(function(e) {
    e.preventDefault();
    $(this).prop('selected', !$(this).prop('selected'));
    return false;
});

The problem is that the code is not working on FireFox 31.0. you can try it using the following link

FIDDLE

does anybody know a work around for this problem :)

like image 276
user3584625 Avatar asked Jun 27 '26 15:06

user3584625


1 Answers

The below code works in firefox 31.0,IE 10 and crome 36.0.1985.143. But it dose not work well if CTRL keys is used also.

 $('select').bind("click", function (event, target) {

        event.preventDefault();
        var CurrentIndex = event.target.selectedIndex==undefined? $(event.target).index(): event.target.selectedIndex
        var CurrentOption = $("option:eq(" + CurrentIndex+ ")", $(this));
        if ($(CurrentOption).attr('data-selected') == undefined || $(CurrentOption).attr('data-selected') == 'false') {
            $(CurrentOption).attr('data-selected', true);
        }
        else {
            $(CurrentOption).prop('selected', false).attr('data-selected', false);
        }

        $("option", $(this)).not(CurrentOption).each(function (Index, OtherOption) {
            $(OtherOption).prop('selected', ($(OtherOption).attr('data-selected') == 'true') ? true : false);
        });
         return false;
    });
like image 164
Priyank Avatar answered Jun 30 '26 04:06

Priyank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!