Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

jQuery: Select element where enter pressed

Using
Using jQuery, how can I select the input where enter was pressed?

If a user presses enter while focused on an element, I want that input to have a red background.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Examples</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {

 $('.box').keypress(function(e) {
    if(e.which == 13) {
        $(?????????????????).css('background','red');
        return false;
    }
 });
});


</script>

</head>
<body>

<div class="box">
   <div class="title"> <strong>Discount Code</strong>
      <input type="text" name="x1"/>
      <input type="text" name="x2"/>
      <input type="text" name="x3"/>
   </div>
</div>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial