agungprasetyosakti / js-hotkeys

Automatically exported from code.google.com/p/js-hotkeys
0 stars 0 forks source link

pass parameters to callback function #11

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. create in cycle 10 text input boxes
2. create function which will copy selected text to textbox
3. bind Ctrl+0 to Ctrl+9 to assign this function to the textboxes
4. the problem is that you need to create 10 functions to solve this with
js-hotkeys

I suggest to add add parameter 'params' and send it to callback function
like you do with propagate:
this.all[opt.target].events[opt.type].callbackMap[combi] =  {cb: callback,
propagate:opt.propagate, params:opt.params};

Of course 'params:null' needs to be added to the default options.

This works for me.

Martin

Original issue reported on code.google.com by martin.m...@gmail.com on 16 Dec 2007 at 10:05

GoogleCodeExporter commented 8 years ago
@ Martin
will you kindly post you implementation so I can copy it into the source base?

Original comment by Afro.Sys...@gmail.com on 16 Dec 2007 at 10:33

GoogleCodeExporter commented 8 years ago
This is what works for me. I downloaded version 0.0.3 from code.google.com and
changed it to what is attached.

I use it like this:
// have something like: 
<input id="copyHere1" type="text" name="f1" value="" />
<input id="copyHere2" type="text" name="f2" value="" />

// then function 
var copyit = function () {
    var selObj = window.getSelection();
    var text = "";
    if (!selObj.isCollapsed) {
        text = selObj.toString();
    }
    $("#" + this.params).val(text);             
}

// and bind it like
$.hotkeys.add('Ctrl+1', {params: 'copyHere1'}, copyit);
$.hotkeys.add('Ctrl+2', {params: 'copyHere2'}, copyit);

Original comment by martin.m...@gmail.com on 16 Dec 2007 at 10:52

Attachments:

GoogleCodeExporter commented 8 years ago
Instead of: 
$.hotkeys.add('Ctrl+1', {params: 'copyHere1'}, copyit);
do:
$.hotkeys.add('Ctrl+1', {target: 'copyHere1'}, copyit);

params is the parameter name not the member name of the object

Original comment by Afro.Sys...@gmail.com on 6 Jan 2008 at 9:40