Event.originalEvent undefined in focal event with jQuery autocomplete

I need to override the autofill focus event using a custom action and do something depending on whether the user selected from the list using the keyboard, not the mouse. To do this, I check the originalEvent.type file in the event object, which should contain the type of action to be performed (keydown, keyup, mouseenter, etc.).

However, the originalEvent object seems undefined, and I cannot understand why. It works fine in a real focus event in the autocomplete code, but not when I redefine this event inside an autocomplete object.

See my code below. I would appreciate any help on this, as it makes me bananas.

$( "#tags" ).autocomplete({ source: availableTags, focus: function(event, ui) { //Check whether focus was triggered by a mouse or keyboard event if ( /^key/.test(event.originalEvent.type) ) { //Do something here } return false; } }); 
+4
source share
2 answers

I really managed to get to the originalEvent , but I don’t think it will help you, even if you can get it; look at the console in the following example: http://jsfiddle.net/andrewwhitaker/DkumP/1/

You will notice that type of originalEvent is menufocus , which I don't think you expected.

A possible way to accomplish what you are trying to do is to check event.which and see if there is an up arrow or a down arrow (provided that these are the only keys that you can use in autocomplete widgets):

 $("input").autocomplete({ source: /*...*/, focus: function(event, ui) { /* If the key that triggered this event is up or down arrow: */ if (event.which === 38 || event.which === 40) { console.log('key'); } else { console.log('mouse'); } } }); 

Here is an example: http://jsfiddle.net/andrewwhitaker/DkumP/2/

+2
source

Thanks for the offer, Andrey.

I forgot to mention that I used jQuery UI 1.8.5, so checking the β€œwhich” property did not work for me, as it was undefined. Upgrading to 1.8.10 helped, and it began to work. Apparently, all this was a bug in 1.8.5, which was fixed later.

Thus, if someone else struggles with a similar problem - is updated to the latest version, this should solve the problem.

0
source

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


All Articles