HTML onselect Event Attribute

The HTML onselect event attribute is triggered when a user selects some text of an HTML element in an HTML document.


Following is the syntax −

<tagname onselect=”script”></tagname>

Let us see an example of HTML onselect event Attribute −


 Live Demo

<!DOCTYPE html>
   body {
      color: #000;
      height: 100vh;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
      text-align: center;
      padding: 20px;
   textarea {
      border: 2px solid #fff;
      background: transparent;
      padding: 10px;
   ::placeholder {
      color: #000;
<h1>HTML onselect Event Attribute Demo</h1>
<textarea onselect="selectFn()" rows="5" cols="50">Please select me because I'm dummy text.</textarea>
   function selectFn() { = "linear-gradient(45deg, #8BC6EC 0%, #9599E2 100%) no-repeat";
      document.querySelector("textarea").style.background = "#fff";


Now select the text inside the text area element to observe how onselect event attribute works.