user3584625
user3584625

Reputation: 259

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 :)

Upvotes: 3

Views: 5239

Answers (1)

Priyank
Priyank

Reputation: 1384

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;
    });

Upvotes: 1

Related Questions