views:

143

answers:

3

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
});

Thanks much in advance.

+3  A: 

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
Not so, the first example is a self executing `FunctionExpression` which will result in an error due to a full stack.
Sean Kinsey
@Sean: Fixed that. Copy/paste error.
Daniel Vassallo
A: 

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
@mkoryak: 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
Will crash the browser due to infinite loop.
Khnle
This is going to cause a stack exception as `dropdownSelect` is a `FunctionExpression` that calls itself.
Sean Kinsey
A: 
var dropdownSelect = function(event, ui) { ... };
var onDropdownSelect = function(event, ui) { dropdownSelect(event, ui) };
$("#auto").autocomplete({
    source: "somepage.php",
    select: onDropdownSelect,
    minLength: 0
});
Khnle