Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JQUERY - capture enter key press

Posted on 2012-09-03
4
Medium Priority
?
1,325 Views
Last Modified: 2012-09-03
How do i capture enter key press in the following?  
(what I'm trying to do is have it close the JQUERY UI dialog when the enter is pressed)

$("#dialog-save").dialog({
   autoOpen: false,
   modal: true,
   height: 185,
   width: 300,
   buttons: {
		Ok: function() {
			$( this ).dialog( "close" );
			location.reload(true);
		}
	}
});

Open in new window

0
Comment
Question by:EricLynnWright
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 38360673
Try this - add this as an event handler - not in the button function above
$(function() {
  $(document).keypress(function() {
    $('#dialog-save").dialog("close");
  });
});

Open in new window

like this
$(function() {
 ...
  $(document).keypress(function() {
    $('#dialog-save").dialog("close");
  });

  $("#dialog-save").dialog({
    autoOpen: false,
    modal: true,
    height: 185,
    width: 300,
    buttons: {
		Ok: function() {
			$( this ).dialog( "close" );
			location.reload(true);
		}
	}
  });
});

Open in new window

0
 
LVL 3

Author Comment

by:EricLynnWright
ID: 38360693
Thanks, julian.

However, it prevented the dialog from functioning.
0
 
LVL 3

Author Closing Comment

by:EricLynnWright
ID: 38360707
I was able to get this to work after seeing your suggestions.  
Thanks for the help!



$('body').keyup(function( e ) {
  if( e.keyCode == $.ui.keyCode.ENTER ) {
      if( $("#dialog-save").is(":visible") ) {
                        $("#dialog-save").dialog( "close" );
                        location.reload(true);
      }
  }
});
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38360710
Sorry missed that thought you said any key

here you go
$(function() {
 ...
  $(document).keypress(function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
      if(code == 13) { //Enter keycode
        $('#dialog-save").dialog("close");
      }
  });

  $("#dialog-save").dialog({
    autoOpen: false,
    modal: true,
    height: 185,
    width: 300,
    buttons: {
		Ok: function() {
			$( this ).dialog( "close" );
			location.reload(true);
		}
	}
  });
});

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question