David
David

Reputation: 111

selecting multiple elements using shift and mouse click - jquery

Is it possible to use shift and mouse click to select multiple elements on a page using jquery?

I have several divs that i have given a tabindex to so that i can select them and can do things like delete them etc.

I want to be able to select more than 1 by holding down shift and using the mouse to click on each div and am struggling to do this.

Does anyone know how this can be done?

Upvotes: 11

Views: 16922

Answers (5)

Mike
Mike

Reputation: 1811

I have a jQuery plugin that does exactly what you want it is called finderSelect it enables Shift+Click, Ctrl+Click, Ctrl+Click+Drag and Standard Clicking on any element.

Upvotes: 4

Ishan Jain
Ishan Jain

Reputation: 8171

To be honest, the Ctrl + left click for selecting multiple items is pretty standard UI behaviour and built-in to the jQueryUI Selectable. Did you also know you can left click and drag a focus over multiple items to select them?

However, I can see an advantage in offering the behaviour in question, so how about using left click or drag to select and then left click and drag to also de-select?

It may not be the most efficient way of doing it, but after playing around with the in-built callbacks, I've come up with something that seems to work. Based on the code in your question I've hooked into the in-built callback functions to store what was selected and also handle the selection removal. JavaScript duplicated below but

Upvotes: 0

joao
joao

Reputation: 342

I did something like that some time ago, with jQuery:

$(id).click(function(event){    //Mouse Click+shift event 
        if(event.shiftKey){
                     //give some attribute that can indentify the elements of the selection
                     //example rel='multi-selection' or class='multi-selection'
        }
});

Then you should do functions that select this elements and do whatever you need, I used this to drag multiple elements. Example if you want to delete this divs, you can for example:

function deleteMultiSelection(){
    $('html').find('div[rel=multi-selection']).each(function(){
          $(this).remove();
     })

} 

$("#button").click(function(){
    deleteMultiSelection();
})

Be careful because I didn't test this code.

Upvotes: 9

Nick Craver
Nick Craver

Reputation: 630449

It sounds like jQuery UI Selectable is what you're after, you can try it out here.

To stay with OS conventions, they key it uses is Ctrl and not Shift, this isn't an option you can change without changing the jQuery UI code itself. It also has the feature of click and drag over elements to get a rectangular selection as well...if that's of any use.

Upvotes: 1

unomi
unomi

Reputation: 2672

Sure, if you are willing to do some work :)

Listen for the shift keydown, set a var that you can access from within your click handler functions, if the var is set then add that item, (or their tabindex for your current implementation) to your list of items to operate on when an 'action button' is pressed.

unset the var when you get the shift keyup event.

Upvotes: 0

Related Questions