Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if "change" event is from keyboard

I have on change trigger that gets the event :

  $(document).on('change', '.class', function (eve) {

How can I know if the change is from the mouse or from the keyboard by the event variable?

like image 276
rikush Avatar asked Nov 08 '22 17:11

rikush


1 Answers

No, that is not possible because a change event may only fire when the concerned element loses focus, after several changes were made with several different input methods, including mouse and keyboard.

Test it with this input element: enter some text with keyboard, paste some text using the context menu (right click), and maybe move some characters by dragging them with the mouse into a different position. Then press the button. Only now you get the change event:

$(document).on('change', '.class', function (eve) {
    console.log('change event occurred');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="class">
<button>Button</button>
like image 100
trincot Avatar answered Nov 15 '22 06:11

trincot