Thursday, October 16, 2025
HomeLanguagesJavascriptJavaScript onmouse Events

JavaScript onmouse Events

The onmouse event is used to define the operation using the mouse.

JavaScript onmouse events are:

JavaScript onmouseover and onmouseout: The onmouseover and onmouseout events occur when the mouse cursor is placed over specific element

Example 1: These events do not require a mouse click to happen

HTML




<script type="text/javascript">
    function over()
    {
        document.getElementById('key').innerHTML=
    "Onmouseover event has occurred";
    }
    function out()
    {
        document.getElementById('key').innerHTML=
    "Onmouseout event has occurred";
    }
</script>
  
<h2 id="key" onmouseover="over()" onmouseout="out()">
    Original Text
</h2>


Output:

 

JavaScript onmouseup and onmousedown:

Example: These events occur when a mouse click is encountered

javascript




<script type="text/javascript">
    function up()
    {
        document.getElementById("gfg").innerHTML="Welcome to GFG";
    }
    function down()
    {
        document.getElementById("gfg").innerHTML = "Goodbye";
    }
</script>
<p id="gfg" onmouseup="up()" onmousedown="down()">Hello</p>


Output:

 

JavaScript onmouseenter and onmouseleave: The onmouseenter event occurs when the mouse is placed on the element and stays until the mouse is removed from the element onmouseleave event occurs as soon as the mouse is removed from the element

Example: These event do not require mouse click to happen

javascript




<script>
    function enter(){
            document.getElementById("gfg").innerHTML="Enter";
    }
    function leave(){
            document.getElementById("gfg").innerHTML = "Exit";
    }
</script>
<p id="gfg" onmouseenter="enter()" onmouseleave="leave()">
    Welcome to GFG
</p>


Output:

 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS