views:

219

answers:

5

I have multiple fields, typically enter will be pressed on one of the two main ones. I want to know which field enter has been pressed on, how do i do this? (i dont know much JS)

Possible Duplicates

+2  A: 

Check for enter and set some hidden field (example uses JQuery):

$('#input_text').keyup(function(e) {
    //alert(e.keyCode);
    if(e.keyCode == 13) {
     alert('Enter key was pressed.');
    }
});
jsight
+4  A: 

its simple to add an "onkeypress" event to each of the fields, and then in the event handler to examine the keycode that is attached to the event. For example, consider the following code:

form.elements['fieldone'].onkeypress = function(evt) {
   if (window.event) evt = window.event; // support IE
   if (evt.keyCode == 13) alert("Enter was pressed!");
   return true;
}

Please note that under most browsers, pressing ENTER in a form field would post that form. If you don't want that to happen, you can simply return false from the onkeypress handler and that would tell the browser to ignore that key.

Guss
I do want it to post but i want to know which field enter was hit. If i change a form value will it run before posting the data?
acidzombie24
Indeed it will.
Guss
+1  A: 

Include this in your page, it should fire automatically when you hit any key and tell you which html element had focus when it happened.

<script>
document.onkeypress = KeyPressed;

function KeyPressed(e)
{
    if (!e) e = window.event;

    f ((e.charCode) && (e.keyCode == 13))
    alert('Yay! Enter was pressed while field ' + document.activeElement.id + ' had focus!');
}
</script>
Mr. Smith
A: 

You can check from which element the event bubbled from using something like the following

var text1 = document.getElementById('text1');
var text2 = document.getElementById('text2');
text1.onkeypress = keyPresser;
text2.onkeypress = keyPresser;

function keyPresser(e) {
    // to support IE event model
    var e = e || window.event;
    var originalElement = e.srcElement || e.originalTarget;

    if (e.keyCode === 13) {
      alert(originalElement.id);
    }
}

Here's a Working Demo

I would recommend taking a look at the differences in Browser event models and also at unobtrusive JavaScript .

Russ Cam
Event handlers are called in the context of the object where they are attached, so if you're attaching the event handler directly to the wanted objects' onkeypress property, then you can simply look at the `this` reference and it will be the object where the event fired.
Guss
in IE, the this keyword inside of event listener functions points to the window object, not the current element.
Russ Cam
A: 

Use event delegation to avoid attaching event handlers to a large number of elements:

window.onload = function () {
  document.onkeyup = function (e) {
    e = e || window.event;
    var target = e.target || e.srcElement,
        keyCode = e.keyCode || e.which;

    if (target.tagName.toLowerCase() == 'input' && keyCode == 13) {
      alert('Enter pressed on ' + target.id);
    }
  };
};
CMS
Might want to check target.nodeName === "TEXT" too
Russ Cam