JavaScript Is this the appropriate way to detect the “Enter” key in a input “type=text” field for all (or at least most) browsers?

The snippet below shows the way I've coded an onKeyPress function to call a function when the Enter key is pressed. Will this handle all (or at least most) browsers?

function saveMarginKeyPress(thisArg, theEvent) {
    // If property is present, call the saveButton onClick function and then exit
    if ((theEvent.keyCode) && (theEvent.keycode == 13)) { saveMarginClicked(); return; }
    if ((theEvent.charCode) && (theEvent.charCode == 13)) { saveMarginClicked(); return; }
    if ((theEvent.which) && (theEvent.which == 13)) { saveMarginClicked(); return; }
}

function saveMarginClicked() { 
    alert("You pressed the Enter key");
}
<input id="marginTopText" onkeypress="saveMarginKeyPress(this, event)" name="marginTop" type="text" style="width: 28px" />
Answer:1

Use the keydown or keyup event, and check the keyCode property. IE 5-7 did not trigger the keypress event for the enter key.

function saveMarginKeyPress(thisArg, theEvent) {
    // If property is present, call the saveButton onClick function and then exit
    if (theEvent.keyCode == 13) {
        saveMarginClicked();
    }
}

function saveMarginClicked() { 
    alert("You pressed the Enter key");
}
<input id="marginTopText" onkeydown="saveMarginKeyPress(this, event)" name="marginTop" type="text" style="width: 28px" />
Answer:2

As a functional Javascript developer with only a vague understanding of Haskell I really have a hard time to understand Haskell idioms like monads. When I look at >>= of the function instance (&...

As a functional Javascript developer with only a vague understanding of Haskell I really have a hard time to understand Haskell idioms like monads. When I look at >>= of the function instance (&...

  1. what does bind function return
  2. how does bind function work
  3. what does bind function do in socket programming
  4. what does bind function do in javascript
  5. what does bind function do in react
  6. what does bind function do

I was trying to divide a variable with type "UNDEFINED" and other variable with type "OBJECT" and I was storing the value of the division in the third parameter and when i was checking the type for ...

I was trying to divide a variable with type "UNDEFINED" and other variable with type "OBJECT" and I was storing the value of the division in the third parameter and when i was checking the type for ...

  1. variable income
  2. come up with variable cost

I currently integrating the XDSoft DateTimePicker (http://xdsoft.net/jqplugins/datetimepicker/) JQuery into my website and wish to disable the times that have passed. Currently if i select today then ...

I currently integrating the XDSoft DateTimePicker (http://xdsoft.net/jqplugins/datetimepicker/) JQuery into my website and wish to disable the times that have passed. Currently if i select today then ...

  1. datetimepicker disable time
  2. datetimepicker disable time intervals
  3. datetimepicker disable time picker
  4. datetimepicker disable time selection
  5. datetimepicker disable time range
  6. datetimepicker disable past times
  7. bootstrap datetimepicker disable time
  8. bootstrap datetimepicker disable time intervals
  9. jquery datetimepicker disable time
  10. xdsoft datetimepicker disable time
  11. kendo datetimepicker disable time
  12. bootstrap datetimepicker disable time picker
  13. bootstrap datetimepicker disable time selection
  14. jquery datetimepicker disable time range
  15. bootstrap datetimepicker disable time intervals example
  16. datetimepicker disable specific time
  17. malot datetimepicker disable time
  18. javascript datetimepicker disable time
  19. c# datetimepicker disable time

Here i add a delay in javascript forloop using $timeout. Unexpectedly i got an error saying ReferenceError $timeout is not defined. i am new to angularjs please help me. PLNKR function CompLibrary(...

Here i add a delay in javascript forloop using $timeout. Unexpectedly i got an error saying ReferenceError $timeout is not defined. i am new to angularjs please help me. PLNKR function CompLibrary(...

  1. referenceerror $timeout is not defined