4

I'm not sure if this is doable, but I would like to be able to set a jQuery UI event as a function (directly), as opposed to continuing to wrap in additional function(event, ui) { ... } wrappers.

Hopefully you can see what I'm going for from the example below.

Here is what I would like:

$("#auto").autocomplete({
    source: "somepage.php",
    select: dropdownSelect,
    minLength: 0
});

Now I would think that the above would work, since I'm simply trying to say "continue firing this event, just over to that function". Unfortunately, that will not work, and I'm ending up with this: (and for some reason, a disconnect from all data)

$("#auto").autocomplete({
    source: "somepage.php",
    select: function(event, ui) { dropdownSelect(event, ui) },
    minLength: 0
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lance
  • 5,655
  • 4
  • 30
  • 32
  • 2
    I can guarantee you that there is no difference in those two examples you have given. – Sean Kinsey May 24 '10 at 20:31
  • @Sean: After trying to get a few things (like getting the over-wrapped functions going), I have gotten it to work. I don't know why it wasn't working before (as that was **many** revisions ago. There has to be some difference, though because now the wrapped version loses `$(this)`. – Lance May 24 '10 at 20:39
  • 1
    You may want to check out [this](http://stackoverflow.com/questions/1007340/javascript-function-aliasing-doesnt-seem-to-work), [this](http://stackoverflow.com/questions/2656469/assigning-document-getelementbyid-to-another-function/) and [this](http://stackoverflow.com/questions/2662851/javascript-is-it-posible-for-one-member-of-an-object-to-access-another-member-of) regarding that. – Daniel Vassallo May 24 '10 at 20:44

3 Answers3

4

The following two examples should both work in theory:

var dropdownSelect = function(event, ui) {  
    // Code to select drop down
};

$("#auto").autocomplete({
    source: "somepage.php",
    select: dropdownSelect,
    minLength: 0
});

And this:

function dropdownSelect(event, ui) {  
    // Code to select drop down
};

$("#auto").autocomplete({
    source: "somepage.php",
    select: function(event, ui) { dropdownSelect(event, ui) },
    minLength: 0
});

JavaScript functions are first class citizens, which means that you can treat them like any other object.

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • Not so, the first example is a self executing `FunctionExpression` which will result in an error due to a full stack. – Sean Kinsey May 24 '10 at 20:26
0

sure why not define that function first:

var dropdownSelect = function(event, ui) { dropdownSelect(event, ui) };
$("#auto").autocomplete({
    source: "somepage.php",
    select: dropdownSelect,
    minLength: 0
});
mkoryak
  • 57,086
  • 61
  • 201
  • 257
  • Aside from declaring a separate variable and wrapping my function (that takes the same parameters), that's what I have, and it is not passing through. I guess the the additional layer there is the difference? So no matter what I have to add the wrapper? – Lance May 24 '10 at 20:30
  • Will crash the browser due to infinite loop. – Kevin Le - Khnle May 24 '10 at 20:33
  • This is going to cause a stack exception as `dropdownSelect` is a `FunctionExpression` that calls itself. – Sean Kinsey May 24 '10 at 20:33
0
var dropdownSelect = function(event, ui) { ... };
var onDropdownSelect = function(event, ui) { dropdownSelect(event, ui) };
$("#auto").autocomplete({
    source: "somepage.php",
    select: onDropdownSelect,
    minLength: 0
});
Kevin Le - Khnle
  • 10,579
  • 11
  • 54
  • 80