onselect

The onselect event in HTML is triggered when a user selects text within an <input> or <textarea> element, enabling scripts to respond to this interaction.

onselect

The onselect event attribute in HTML is activated when a user selects text in an <input> field or a <textarea>. This event enables developers to trigger specific actions or scripts in response to text selection, improving user interaction and overall experience.

Syntax

In HTML

index.html
<element onselect="myScript">

In JS

script.js
object.onselect = function(){myScript};

In JavaScript, the addEventListener() function is used.

script.js
object.addEventListener("select", myScript);

Example

::event-onselect ::
index.html
<!DOCTYPE html>
<html>
<body>
Select some of the text: <input type="text" value="Hello world!" onselect="myFunction()">

<script>
function myFunction() {
  alert("You selected some text!");
}
</script>

</body>
</html>

Values

Conclusion

The onselect event improves user interaction by allowing <script> to respond when text is selected within form elements. It enables developers to create dynamic actions based on user selection.