Page 1 of 1

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

Posted: Sun Oct 03, 2010 12:07 am
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!

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

Posted: Sun Oct 03, 2010 12:15 am
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();
   });
});

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

Posted: Sun Oct 03, 2010 1:20 am
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.