Skip to content
Advertisements

JavaScript onmousenter Event

onmousenter:

The JavaScript onmouseenter method works when the mouse is moved over the element.

onmousenter vs onmouseover

Secondly, both onmouseenter and onmouseover fire when the mouse enters the boundary of an element. However, onmouseenter doesn’t fire again (does not bubble) if the mouse enters a child element within this first element.

Syntax:

.onmouseenter


onmousenter Examples

Example 1

onmousenter example
<body>

    <div class="container">
        <h2>onmouseover  Event</h2>
        <div id="box" onmouseenter="overFunction()"></div>
    </div>

    <script>

        var i = 0;

        function overFunction() {
            let box = document.getElementById("box");
            var backgroundC = ["Orange", "Green", "Blue", "Red", "Black", "Brown"];
            if (i < backgroundC.length) {
                box.style.backgroundColor = backgroundC[i];
                i += 1;
            }
            else {
                i = 0;
            }
        }

    </script>
</body>

Example 2

onmousenter example
<body>

    <div class="container">
        <h2>onmouseover Event</h2>
        <div id="box"></div>
    </div>

    <script>

        document.getElementById("box").onmouseenter= function () {
            overFunction();
        };

        var i = 0;

        function overFunction() {
            let box = document.getElementById("box");
            var backgroundC = ["Orange", "Green", "Blue", "Red", "Black", "Brown"];
            if (i < backgroundC.length) {
                box.style.backgroundColor = backgroundC[i];
                i += 1;
            }
            else {
                i = 0;
            }
        }

    </script>
</body>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: