Adamhumbug Posted December 3, 2021 Share Posted December 3, 2021 Hi all, I have an input box that is created dynamically. Before it was created dynamically, it was always in the dom and the following code did what i wanted. $('#serialNumber').keyup(function(e){ if(e.keyCode === 13){ barCode = $(this).val() console.log(barCode); $.ajax({ type: 'post', data: {'ajax' : 'one', 'barCode': barCode }, success: function(resp){ $('#barCodeOutput').prepend(resp); } }) } }) now that it is not part of the dom when the page loads, i am calling it with a key up on the html element. I dont know why i am struggling with this so much but i am really struggling to call the function and pass the keycode into it. I have the following which does give me the keycode but having the second function in the first as it is obviously doesnt do what i want. function barCodeVal(){ $(document).keyup(function(e){ console.log(e.keyCode); }) } I would really appreciate a helping hand on this one - google just shows me react explanations. Quote Link to comment https://forums.phpfreaks.com/topic/314269-html-function-call-on-keyup-get-keycode-from-input/ Share on other sites More sharing options...
Solution Adamhumbug Posted December 3, 2021 Author Solution Share Posted December 3, 2021 I am sorry all, as is always the case i have answered by own question shortly after posting. function barCodeVal(e){ var code = e.keyCode; console.log(e.keyCode); } thanks anyway, i am sure i will be back. Quote Link to comment https://forums.phpfreaks.com/topic/314269-html-function-call-on-keyup-get-keycode-from-input/#findComment-1592407 Share on other sites More sharing options...
Adamhumbug Posted December 3, 2021 Author Share Posted December 3, 2021 I thought i would be back. I now have the following: function barCodeVal(event){ var e = event.keyCode; console.log(e) if(e.keyCode === 13){ barCode = e.val() console.log(barCode); $.ajax({ type: 'post', data: {'ajax' : 'one', 'barCode': barCode }, success: function(resp){ $('#barCodeOutput').prepend(resp); } }) } } The first console log works and shows 13 as the output. The second console log does not fire suggesting that the if statement is not picking this up. Any helpers appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/314269-html-function-call-on-keyup-get-keycode-from-input/#findComment-1592408 Share on other sites More sharing options...
Barand Posted December 3, 2021 Share Posted December 3, 2021 e.keyCode ??? 1 Quote Link to comment https://forums.phpfreaks.com/topic/314269-html-function-call-on-keyup-get-keycode-from-input/#findComment-1592409 Share on other sites More sharing options...
Adamhumbug Posted December 3, 2021 Author Share Posted December 3, 2021 3 minutes ago, Barand said: e.keyCode ??? i tried: if($(e).keyCode === 13){ with no joy. Quote Link to comment https://forums.phpfreaks.com/topic/314269-html-function-call-on-keyup-get-keycode-from-input/#findComment-1592410 Share on other sites More sharing options...
Adamhumbug Posted December 3, 2021 Author Share Posted December 3, 2021 7 minutes ago, Barand said: e.keyCode ??? Got ya! got the keycode twice. Thanks as always. Quote Link to comment https://forums.phpfreaks.com/topic/314269-html-function-call-on-keyup-get-keycode-from-input/#findComment-1592411 Share on other sites More sharing options...
Adamhumbug Posted December 3, 2021 Author Share Posted December 3, 2021 Final solution if it is useful for anyone. function barCodeVal(event){ var e = event.keyCode; var et = event.target if(e === 13){ barCode = et.value console.log(barCode); $.ajax({ type: 'post', data: {'ajax' : 'one', 'barCode': barCode }, success: function(resp){ $('#barCodeOutput').prepend(resp); } }) } } Thanks @Barand Quote Link to comment https://forums.phpfreaks.com/topic/314269-html-function-call-on-keyup-get-keycode-from-input/#findComment-1592412 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.