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.

Syntax

Following is the syntax −

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

Let us see an example of HTML onselect event Attribute −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   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;
   }
</style>
</head>
<body>
<h1>HTML onselect Event Attribute Demo</h1>
<textarea onselect="selectFn()" rows="5" cols="50">Please select me because I'm dummy text.</textarea>
<script>
   function selectFn() {
      document.body.style.background = "linear-gradient(45deg, #8BC6EC 0%, #9599E2 100%) no-repeat";
      document.querySelector("textarea").style.background = "#fff";
   }
</script>
</body>
</html>

Output

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


Advertisements