Skip to content

Instantly share code, notes, and snippets.

@trmtsy
Last active March 10, 2020 10:11
Show Gist options
  • Save trmtsy/5692261 to your computer and use it in GitHub Desktop.
Save trmtsy/5692261 to your computer and use it in GitHub Desktop.
テキストボックスでエンターが押された時に実行されるJavaScript
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>keypress</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body id="content">
<input id="textInput" type="text">
<div id="textOut"></div>
<script type="text/javascript" charset="utf-8">
// テキストエリアでENTERが押された時のイベント
$('#textInput').keypress(function(e) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
console.log('which:' + e.which);
console.log('keyCode:' + e.keyCode);
$('#textOut').text($('#textInput').val());
$('#textInput').val('');
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment