1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

検索フィールド[input type="search"]でキャンセルボタンを押した時のイベントも'search'

Last updated at Posted at 2015-02-25

検索フィールドでキャンセルボタンを押した時もsearchイベントが起きるようです。

cancel.png

なので、キャンセルボタンを押されたのか、リターンキーなどで検索処理をしたいのかは判別した方が良さそうですね。

<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title>search</title>

	<style>
		#search_box {
			font-size: 2em;
			-webkit-appearance: none;
		}
	</style>

	<script>
		window.addEventListener('load', function() {

			document.getElementById('search_box').addEventListener('search', function(e) {
				console.log('value:[' + this.value + ']');
				if ( this.value.length === 0 ) {
					console.log('[cancel]');
					return;
				}
				/*
					search procedure
				*/
			}, false);

		}, false);
	</script>
</head>

<body>
	<h1>検索フィールドのキャンセルボタンイベント</h1>
	<input type="search" id="search_box" placeholder="search word">
</body>

</html>

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?