views:

398

answers:

3

hello, have some divs, on some positions. i want select them with mouse, like when are you selecting object in photoshop. so i want to select group of divs. is that possible with jquery ? thanks

+1  A: 

You can simulate a selected effect by changing the classes of the divs, e.g.:

$('div.selectable').click(function() {
    $(this).addClass('.selected');
});

If you want the div to get unselected after is has been clicked again, you can do something like:

$('div.selectable').click(function() {
    if($(this).is('.selected')) {
        $(this).removeClass('.selected');
    } else {
        $(this).addClass('.selected');
    }
});

or more concisely with the ternary operator:

$('div.selectable').click(function() {
    var $div = $(this);
    $div.is('.selected') ? $div.removeClass('.selected') : $div.addClass('.selected');
});
karim79
+1  A: 

You can use the imgAreaSelect plugin as the base of your code.

Zed
+2  A: 

There's jQuery Drag to Select, which will probably solve your problems.

JorenB
thanks jorenb, that looks nice :)
mm
Good luck with it ;-)
JorenB
have problems to work with it, somebody know how run that example which is on that site ?
mm
ok, already know..
mm
anyway, does somebody know, how to use it with divs ? it works only with ul and li ? thanks
mm
As far as I can see from the examples, you'll need to do this: `$('#parent-element').dragToSelect(selectables: 'div')`, but I can't be 100% sure... Hold on.
JorenB
Yeah, that should do it. If you want to make more types of elements selectable, comma-separate them: `div, li, tr` (the plugin inserts this string straight into a jQuery, so you could use virtually any supported CSS3-selector =D )
JorenB