LiamG_G LiamG_G -4 years ago 80
HTML Question

jQuery: Ho to make pressing enter on text box run a function?

I've been making the Mad Libs game from General Assembly, and I'm trying to split everything into its own files (.html, .css, .js) and add a bit more functionality.

In it, a function is run which produces a text box. You click a button, the value is saved and a new text box appears (these are not forms, just an input and a button).

Currently, pressing the button works, but I want to make it also work when I press enter while the text box is selected.

This is what I have tried:

if(event.which == 13){

but it is not working.

Answer Source

For dynamically generated elements, you can use the $(document) selector :

$(document).on("keydown", "input", function(event){
    if(event.which == 13) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download