jQuery select()

Introduction

Alert a message when a text is selected in a text field:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*from   w w w  . j  a  v a 2 s.  c  om*/
<script>
$(document).ready(function(){
  $("input").select(function(){
    document.getElementById("demo").innerHTML = "Text marked!";
  });
});
</script>
</head>
<body>

<p id="demo"></p>
<input type="text" value="Hello World">

<p>Select some text inside the input field.</p>

</body>
</html>

The select event occurs when a text is selected in a text area or a text field.

The select() method triggers the select event.

The select() method can also run a function when a select event occurs.

Trigger the select event for the selected elements:

$(selector).select()

Attach a function to the select event:

$(selector).select(function)
Parameter Optional Description
function Optional. the function to run when the select event is triggered



PreviousNext

Related