Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery event upon selection of a suggested item on a text box?

I'd like to run a function upon each modification of a textbox control as soon as the user is typing. the event .keyup() is fine for most of the cases.

However, browsers (like Chrome or Firefox) may suggest autocompletion entries for textbox controls (probably because the @name or @id of the input control is known).

Unfortunately, I can't have any of the below events fired when "clicking on a suggested entry". ( .keyup() fires when selected with keyboard )

$('input#email')
  .click(function() { console.log('click'); })
  .keyup(function() { console.log('keyup'); })
  .keydown(function() { console.log('keydown'); })
  .change(function() { console.log('change'); })
  .focus(function() { console.log('focus'); })
  .blur(function() { console.log('blur'); });

As much as possible, I'd like to avoid using a setInterval() periodical check.

Is there a way to detect this "select a suggestion" event ?

like image 902
Myobis Avatar asked Jun 15 '12 10:06

Myobis


People also ask

What is keypress event in jQuery?

The keypress() method triggers the keypress event, or attaches a function to run when a keypress event occurs. The keypress event is similar to the keydown event. The event occurs when a button is pressed down. However, the keypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC).

Which of the following events is generated when the value of textfield or textarea is change?

The input event fires when the value of an <input> , <select> , or <textarea> element has been changed.

How to keyup in jQuery?

The keyup() is an inbuilt method in jQuery which is used to trigger the keyup event whenever User releases a key from the keyboard. So, Using keyup() method we can detect if any key is released from the keyboard. Here selector is the selected element.


3 Answers

bind propertychange event:

$('input').bind('input propertychange', function() { 

    var input = this.value.trim();
    [...]   

});
like image 128
dieter Avatar answered Nov 15 '22 12:11

dieter


The short answer is no, there is no event for detecting a suggestion selection. You could try looking at DOM mutation observers but I'm uncertain if these cover attribute changes, and there is little support for this API so far anyway.

So if you really need to handle this case then I think setInterval is your only option.

Edit: 3 years later you can use propertychange with jQuery as in the new accepted answer (which I assume uses mutation observers under the hood).

like image 42
Graham Avatar answered Nov 15 '22 14:11

Graham


alternative solution disable autocomplete and handle other events like keyup,keydown etc

<input type="text" name="foo" autocomplete="off" /> 
like image 41
Hassan Saeed Avatar answered Nov 15 '22 12:11

Hassan Saeed