Fallenreaper
Fallenreaper

Reputation: 10704

How do i recognize a ctrl+click on an html webpage with javascript/jquery?

I am making an application which makes use of context menus and has selection. Right now i can select 1 element, but what i want to do is to ctrl+click to allow me to say append the elements into an array for the selection of MULTIPLE elements simultaneously. That way i can affect the attributes of N things at the same time.

I Need it to be something like Control+Clicking, if there was a better idea, i could be interested. Maybe Shift+click but that has the general understanding of selecting everything ebtween X and Y, where as users are more familiar with clicking individual items with ctrl.

I know how to do the append thing, but i wasnt sure how to do the:

var ev = mouse||window.event;
var t_sel = ev.target || ev.srcElement;
 ...

Upvotes: 0

Views: 3766

Answers (3)

mortb
mortb

Reputation: 9869

I've used shiftcheckbox to have the ability to select a range of checkboxes in a grid. The code is available so you can alter it to fit your needs. You may also use it as inspiration for a functionallity that suits you. https://github.com/nylen/shiftcheckbox

Upvotes: 0

Māris Kiseļovs
Māris Kiseļovs

Reputation: 17295

$('.item').click(function(e) {
    if (e.ctrlKey || e.metaKey) {
        // required code to make selection
        // propably, add class to item to style it like selected item and check hidden checkbox
        $(this).toogleClass('selected');
        $(this).find('input[type=checkbox]').attr('checked', !$(this).find('input[type=checkbox]')('checked'));
    }
});

Upvotes: 4

Justin Helgerson
Justin Helgerson

Reputation: 25551

This will allow you to detect a control click:

$(document).click(function(e) {
  if(e.ctrlKey) {
    //You do your stuff here.
  }
});

Upvotes: 2

Related Questions