23

I have successfully hooked the EnterKey event at the document level as following:

    $(document).keypress(function (e) {
        if (e.which == 13) {
            alert('You pressed enter!');
        }
    });

But, I am unable to hook the EnterKey event on a Div with id "myDiv".

<div id="myDiv"></div>

$("#myDiv").keypress(function (e) {
     if (e.which == 13) {
         alert('You pressed enter!');
     } 
 });

Is it possible to detect an EnterKey press within a Div? Actually, the Div contains some input/select controls which are used to filter the content of a Grid. I want to hook the EnterKey event so that when the EnterKey is pressed I can filter the Grid.

EDIT: Please note that I have inputs within the div(I've intentionally not shown them here). I don't want to hook the event manually for each of the input control within the Div. Also, I am assuming that when user presses EnterKey at least one input control shall have focus.

2
  • How are you sending a keypress to a div ? I mean, how are you testing this ? Commented May 29, 2013 at 11:00
  • 1
    the keypress can be only detected globally on $(window) and form inputs. Commented May 29, 2013 at 11:02

8 Answers 8

35

Try this

 <div id="Div1">
    <input type ="text" id="aa"/>
    sdfsd hsjdhsj shdj shd sj
 </div>

Jquery

$(function(){
 $("#Div1 input").keypress(function (e) {
    if (e.keyCode == 13) {
        alert('You pressed enter!');
    }
 });
});

Demo

Sign up to request clarification or add additional context in comments.

Comments

3

The only way a DOM element can receive key events, is if he has the focus.

Either set the focus to your div in the onready handler :

$(function() {
   $('#mydiv').focus();
});

Or give the focus to your element through the tabindex attribute.

EDIT : As @roasted explained, you can also set a CSS rule to your div to avoid restyling issues :

#myDiv {
  outline: 0 solid;
}

3 Comments

but it will lose the focus very easily ;-)
could use in addition css: #myDiv{outline:0} to avoid active element restyling
Sure, but he will at least be able to receive events. This is why key events are usually handled by inputs elements.
3

What you really need to do here is bind the keypress event to all form elements such as input, select and textarea inside the div like:

$("#myDiv input, select, textarea").keypress(function (event) {
    if (event.which == 13) {
        event.preventDefault();
        alert('You pressed enter!');
    }
});

Main thing to notice here is the use of event.preventDefault(). Since, if you don't use it, pressing the enter key might result in submitting the form, which you obviously don't want here. You just want to press the enter key to filter the content of a Grid. So, as soon as the enter key is pressed you can get the values from the input elements and filter the grid accordingly.

Comments

1

You can focus the submit button or a tag first then click it

  $('#id of input button').focus();

or

 $('input[type="submit"]').focus();

then press enter

 $("#inputid").keypress(function (e) {
   if (e.which == 13) {
    alert('You pressed enter!');
  }
});

Comments

0

Your question could be more precise - you want to detect KeyPressed event on input object nested withing certain div. Maybe you could try:

$("#myDiv input,select").keypress(function (e) {
        if (e.which == 13) {
            alert('You pressed enter!');
        }
    });

1 Comment

Look at the the original question: "Actually, the Div contains some input/select controls which are used to filter the content of a Grid"
0

Enter key is pressed mainly to input something or submit. Have a button or text box inside the div which is highlighted and then press enter.

Comments

0
$("#inputid").keypress(function (e) {
    if (e.which == 13) {
        alert('You pressed enter!');
    }
});

you want to abind the event handler to a specific input / button (like some "filter" button)

1 Comment

You shoul rather use $("#inputid") rather than $("#myDiv #inputid") in this case. Id is unique for each element on page, there's no need to specify it's parent in the selector.
0

You can first set the focus on the div using:

window.location.hash = '#name of div';

Then use your code it will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.