Coly010 Posted August 2, 2013 Share Posted August 2, 2013 The title might sound confusing, but in essence it isn't. I want to have a JQuery function that posts the data in a textbox and then resets the value of the textbox when the enter button is pressed. I know that to post the data I use JQuery's $.post(); which i have now become familiar with. However my problem is finding whether or not the enter button has been pressed when the textbox is in focus. I know that I would give the textbox an id, then use a JQuery selector to select the element, then use an if statement to check if the enter button is pressed. I know that there is a function in JQuery .keyPressed(event); but how do I use the event parameter to find out if the enter button has been pressed. A google search returned me with the following result: $('#textbox').keypress(function(event){ var keycode = (event.keyCode ? event.keyCode : event.which); if(keycode == '13'){ alert('You pressed a "enter" key in textbox'); } }); and $('#txt').keydown(function (e){ if(e.keyCode == 13){ alert('you pressed enter ^_^'); } }) but neither of these seem to work, and yes I have changed the id of the selector. it just doesn't seem to be able to respond to the enter button being pressed. I would prefer to be able to be able to use a <textarea> if that makes a difference. Any help would be seriously appreciated. Quote Link to comment Share on other sites More sharing options...
Dayo Posted August 2, 2013 Share Posted August 2, 2013 http://jsfiddle.net/VuwB9/1/ that works for me Quote Link to comment Share on other sites More sharing options...
Coly010 Posted August 13, 2013 Author Share Posted August 13, 2013 It still isnt working, I know that it should, which means it must not be working in conjunction with the rest of my jquery :/ Quote Link to comment Share on other sites More sharing options...
Coly010 Posted August 13, 2013 Author Share Posted August 13, 2013 Sorted it out, thanks for the help Dayo, it does work :) Quote Link to comment Share on other sites More sharing options...
HauntedDawg Posted August 13, 2013 Share Posted August 13, 2013 $('#textarea').bind('keypress', function (event){ if(event.keyCode == 13){ alert('you pressed enter ^_^'); } }) Works perfect for me, and make sure your input element/textarea element has an ID of textarea. Quote Link to comment Share on other sites More sharing options...
Coly010 Posted August 14, 2013 Author Share Posted August 14, 2013 I had. The problem was that i was loading a file using jquery, which didnt have the js file linked to it. I had expected that because i had it linked in the header file, and because jquery was loading the file onto a page which included the header file, the js functions would be able to be accessed by the file getting loaded by jquery Quote Link to comment 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.