enter key in text(type) input(element name)

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
kolmes911
Forum Newbie
Posts: 5
Joined: Sat Sep 25, 2010 5:41 pm

enter key in text(type) input(element name)

Post by kolmes911 »

I want to call a function when Enter key is pressed in a input text area when the cursor is flashing.

How would you solve this?

Also, I want it to be functional on both FF and IE.

Thank youuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: enter key in text(type) input(element name)

Post by John Cartwright »

I would use jquery to greatly simplify the task and cross browser compatability issues.

I.e.,

Code: Select all

$(function() {
   var inputBox = $('#id_of_your_input');
   var keyCode = null;
   
   inputBox.keypress(function(e) {
      keyCode = (e.keyCode ? e.keyCode : e.which);
      if (keyCode == 13) {
         alert('Enter key pressed.');
      }
      e.preventDefault();
   });
});
kolmes911
Forum Newbie
Posts: 5
Joined: Sat Sep 25, 2010 5:41 pm

Re: enter key in text(type) input(element name)

Post by kolmes911 »

Thank you, John.
But, I could somehow get it to work testing many codes.
Though I'm aware that this is really bad programming, I use this for now.
I'm a real newbie to Javascript and PHP with self-learning, by the way.

// for FF
elementName.onkeydown=function(e){
if(e.keyCode==13){
functionName();
return false;
}
};

//for IE
elementName.onkeyup=function(){
if(window.event.keyCode==13){
functionName();
return false;
}
};


And, I'll come to see your code when the time comes that I'm really ready to learn JQuery.
Post Reply