JavaScript function forwarding?

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

I hope you see what I will do from the example below.

Here is what I would like:

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

Now I would think that this would work, as I was just trying to say, "Keep firing this event, right down to this function." Unfortunately, this will not work, and I get the following: (and for some reason disconnects from all data)

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

Thank you very much in advance.

+4
source share
3 answers

The following two examples should work theoretically:

 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.

+4
source

Of course, why not define this function first:

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

Source: https://habr.com/ru/post/1310779/


All Articles