1

I have a focused TextBox and the user will use a Barcode reader device to fill it with the decrypted data. How can i call a function immediately after the text appears in the TextBox without having to loose its focus first?

enb081
  • 3,831
  • 11
  • 43
  • 66
  • @DJDavid98 I have no idea how it works but the text just appears wherever the cursor is. – enb081 Mar 01 '13 at 22:57
  • 1
    It might be simulating keystrokes. Try `.keyup()` – Austin Mullins Mar 01 '13 at 23:01
  • @DJDavid98 I have tried `$(".txt").change(function(e) {`, `$('.txt').bind('paste', function(e) {`, and `$('.txt').keyup(function(e) {` Thank you for your very informative comment. – enb081 Mar 01 '13 at 23:02

1 Answers1

2

Missing some detail, but here it is:

According to your comment, I think the device simulates typing when reading the code and inserting that into the text box. You can catch that using keyup, change and keydown:

$('.txt').on('keyup keydown change',function(){
    //code to run
}

If the above method doesn't work then I can't really think of anything else. If it just plain inserts the text, then it has to fire at least one of the events.


In reply to your second comment:

If the value is changed using JavaScript, then you should use that function that changes the value to run code handling the change, or link both of the approaches to the same function, like:

function iChangeTheValue(){
    $('.txt').val('0231231212321');
    changedText();
}

$('.txt').on('keyup keydown change',function(){
    changedText();
}

function changedText(){
    //code
}

About the autofill part: You can disable it if it breaks the code using a html attribute autocomplete="off". Otherwise, please read this.

Community
  • 1
  • 1
  • If you, for instance, change the value of the textbox via Javascript, say `$('.txt').val("123");`, or fill you select one of the values that the browser autocomplete suggests is there anyway I can catch the event of this change? I am looking for something general. Something like 'ontextchanging' but not necessarily via keyboard. – enb081 Mar 01 '13 at 23:09