jQuery - selectイベント

公開日:2018-12-21 更新日:2019-05-14
[jQuery]

1. 概要

テキストボックスの文字列を選択すると、selectイベントが発生します。

$("#text1").on("select", function(e) {
	処理;
});

2. サンプル

テキストを選択すると、メッセージが表示されます。

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>jQuery</title><style>body{color:#fff; background-color:#000;}</style></head>
<body>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

  <input    id="text1" type="text" value="1234567890" /><br /><br />
  <textarea id="text2" cols="40" rows="5">1234567890</textarea>
  <div id="msg"></div>

  <script>

    $("#text1").on("select", function(e) {
      $("#msg").html("input のテキストが選択されました。");
    });

    $("#text2").on("select", function(e) {
      $("#msg").html("textarea のテキストが選択されました。");
    });

  </script>
</body></html>

実行結果:画面