Is there a way to change event parameters in jQuery?
Asked Answered
M

3

4

I want to be able to change the parameters that are being passed into the selectable's start event, so I can basically allow my user to use the selectable jQuery UI effect without having to hold down the CTRL key.

JS

$(function() {
$( "#selectable" ).bind("mousedown", function(event, ui) {
    var result = $( "#select-result" ).empty();
    event.metaKey = event.ctrlKey = true;
});
$( "#selectable" ).selectable();
});

I have a fiddle with what I'm trying to accomplish here:

http://jsfiddle.net/josephbulger/ZfevM/

The problem I'm having, is that when i set the event's parameters in the start method, the stop method is not seeing the changes I'm making.

Is there a way to accomplish what I'm trying to do?

Marylou answered 18/11, 2010 at 13:39 Comment(1)
If the chosen correct answer doesn't work for you, try this one. https://mcmap.net/q/536051/-is-there-a-way-to-change-event-parameters-in-jqueryDisqualify
C
15

You can't set the properties here no...because it's a different event object in the stop method. You can set some variables at a higher scope (like this), but none that will persist on the event object. It's not that it's "clearing" them really, it's just a brand spanking new object.


To make the selectable behave as if Ctrl is held down, bind to the mousedown event it uses ahead of time and set the .metaKey property on that event to true, like this:

$("#selectable").bind("mousedown", function(e) {
    e.metaKey = true;
}).selectable();

You can test it out here, remember to find before calling .selectable(), since event handlers are executed in the order bound.

Chant answered 18/11, 2010 at 13:43 Comment(5)
@Nick so where is the higher scope that I need to tell the jQuery object to always use CTRL when a the user clicks on an item?Marylou
@Marylou - I added an example of this, just some variables in the scope that's the common parent, for example in the document.ready like you have...or even as .data() properties on the selectable container...wherever you want to put them really.Chant
@Nick I see what you did, but that isn't causing the jQuery Selectable effect to use those values. What I want it to do is to have the jQuery selectable object to actually "think" the user was holding down the CTRL key. Is that possible?Marylou
@Marylou - Ah, that's a different question :) Yes you can bind the mousedown event ahead of time, like this: jsfiddle.net/nick_craver/ZfevM/3 Is that what you're after?Chant
@Nick Perfect!! Can you add that fiddle to your answer so others can see what the solution was?Marylou
S
5

I've updated the code. Have a look at this URL.

http://jsfiddle.net/phoenix_suresh/7f6j5/

Meanwhile, I've added ID attribute to

  • items so that you can have more control on it.
  • Synthetic answered 7/9, 2011 at 9:44 Comment(0)
    C
    4

    After finding this answer I noticed that you can use the originalEvent property on the event object returned in the selectable start event handler to set the ctrlKey property to true.

    Like so:

    $("#selectable").selectable({
        start: function (event, ui) {
            event.originalEvent.ctrlKey = true;
        }
    });
    

    Working fiddle

    Curtsy answered 2/6, 2015 at 13:23 Comment(1)
    The accepted answer didn't work for me, but this did. Thanks!Eggert

    © 2022 - 2024 — McMap. All rights reserved.