JavaScript Lessons

JavaScript onselect Event and onmousemove Explained

See how JavaScript's onselect and onmousemove events work with easy examples. Track text selection and follow the mouse pointer in your own projects.

Serhat ALPASLAN2 min read

javascript operators

Javascript onSelect

With Javascript onSelect, you can trigger a JavaScript function when the text inside text boxes is selected. onSelect supports HTML tags such as <input type=”file”>, <input type=”password”>, <input type=”text”> and <textarea>. Also, onSelect does not bubble and cannot be canceled.

<html>

<head>
<title> onSelect Kullanımı </title>
<script>
function f()
  { 
   alert("Text kutusu içerisindeki yazıyı seçtiniz .");
  }
</script>
</head>
<body>
<p>Önce bir yazı yazın ve yazdığınız yazıyı seçin</p>
<input type="text" onselect="f()"/>
</body>
</html>

As you can see in the example above, you typed text into the text box and, after selecting that text, the onselect event defined on the text box fired and you saw the alert message.

 

Javascript onMouseMove

It lets you trigger a JavaScript function whenever the mouse is moved over any HTML DOM element. To give an example, imagine you’ve built a page and that page contains an image; when you move the mouse over that image, you can trigger a JavaScript function. Let’s build that example as shown below;

 

<!DOCTYPE html>
<html>
<head>
<script> 
function fnkMouseMove() {
  alert="Görsel Üzerinden hareket ettiniz ."
}
</script> 
</head>
<body>
<img src="dosyaAdi.png" onmousemove="fnkMouseMove()"></div>
<p>Fareyi yukardaki gorsel üzerinde gezdirin ve alert mesajını tetikleyin.</p>
</body>
</html>

Share it if it helped

Write a comment