Jump to content

Html function call on keyup - get keycode from input


Go to solution Solved by Adamhumbug,

Recommended Posts

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.

  • Solution

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.

 

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.