Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 343
David Avatar asked Oct 02 '10 21:10

David


People also ask

Can you select multiple elements in jquery?

Definition and Usage. The element selector can also be used to select multiple elements. Note: Seperate each element with a comma.

How do you shift to select multiple items?

If you want to select items that are adjacent, you can use the SHIFT key. Click the first item, then press the SHIFT key and hold it. Click the last item and release the SHIFT key. To select adjacent items, you can also use the mouse.

How do you select multiple elements in Javascript?

To select multiple elements, you can use the querySelectorAll() method. Just like the querySelector() method, you usually use it on the document object.

How do you click more than one option?

Windows: We need to hold down the CTRL button to select multiple options. Mac: We need to hold down the command button to select multiple options.


3 Answers

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.

like image 135
joao Avatar answered Nov 15 '22 12:11

joao


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.

like image 32
Mike Avatar answered Nov 15 '22 13:11

Mike


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.

like image 40
Nick Craver Avatar answered Nov 15 '22 13:11

Nick Craver