Skip to content
Advertisements

JavaScript onmouseout Event

Why onmouseout ?

The JavaScript onmouseout feature works if the mouse goes out of the specified area. The mouse clogging event is triggered when the mouse releases an element. For example, when the mouse moves from an image on a web page, the mouse-related event rises for that image element.

Syntax:

HTML Attribute<element onmouseout = “handler(event)“>
Event Propertyobject.onmouseout = handler;
attachEvent Methodobject.attachEvent(“onmouseout“, handler)
addEventListener Methodobject.addEventListener(“mouseout“, handler, useCapture)

onmouseout Examples

Example 1

onmouseout example 1
<body>

    <div class="container">
        <h2>onmouseout Event</h2>
        <div id="box" onmouseout="moveFunction()"></div>
    </div>

    <script>

        var i = 0;
        
        function moveFunction() {
            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

onmouseout example 2
<body>

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

    <script>

        document.getElementById("box").onmouseout = function () {
            moveFunction()
        }

        var i = 0;
        
        function moveFunction() {
            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 3

onmouseout example 3
<body>

    <div class="container">
        <h2>onmouseoutEvent</h2>
        <div id="box" ></div>
    </div>

    <script>

        document.getElementById("box").addEventListener("mouseout", moveFunction)

        var i = 0;
        
        function moveFunction() {
            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 4

onmouseout example 4
<body>

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

    <script>

        let box = document.getElementById("box")
        box.addEventListener("mouseout", moveFunction);
        box.addEventListener("mouseover", overFunction);

        function moveFunction() {
            box.style.backgroundColor = "red"
        };

        function overFunction() {
            box.style.backgroundColor = "orange"
        };

    </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: