#### version added: 1.7 [.on( "mouseleave" \[, eventData \], handler )](https://api.jquery.com/mouseleave/?utm_source=chatgpt.com#on-%22mouseleave%22-eventData-handler) - **"mouseleave"** Type: string The string `"mouseleave"`. - **eventData** Type: Anything An object containing data that will be passed to the event handler. - **handler** Type: Function( Event eventObject ) A function to execute each time the event is triggered. This page describes the `mouseleave` event. For the deprecated `.mouseleave()` method, see `.mouseleave()`. The `mouseleave` JavaScript event is proprietary to Internet Explorer. Because of the event's general utility, jQuery simulates this event so that it can be used regardless of browser. This event is sent to an element when the mouse pointer leaves the element. Any HTML element can receive this event. For example, consider the HTML: | | | | --- | --- | | 1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10 | ```<br><div id="outer"><br> Outer<br> <div id="inner"><br> Inner<br> </div><br></div><br><div id="other"><br> Trigger the handler<br></div><br><div id="log"></div><br>``` | Figure 1 - Illustration of the rendered HTML The event handler can be bound to any element: | | | | --- | --- | | 1<br>2<br>3 | ```<br>$( "#outer" ).on( "mouseleave", function() {<br> $( "#log" ).append( "<div>Handler for `mouseleave` called.</div>" );<br>} );<br>``` | Now when the mouse pointer moves out of the `Outer` `<div>`, the message is appended to `<div id="log">`. You can also trigger the event when another element is clicked: | | | | --- | --- | | 1<br>2<br>3 | ```<br>$( "#other" ).on( "click", function() {<br> $( "#outer" ).trigger( "mouseleave" );<br>} );<br>``` | After this code executes, clicks on `Trigger the handler` will also append the message. The `mouseleave` event differs from `mouseout` in the way it handles event bubbling. If `mouseout` were used in this example, then when the mouse pointer moved out of the `Inner` element, the handler would be triggered. This is usually undesirable behavior. The `mouseleave` event, on the other hand, only triggers its handler when the mouse leaves the element it is bound to, not a descendant. So in this example, the handler is triggered when the mouse leaves the `Outer` element, but not the `Inner` element. Show number of times mouseout and mouseleave events are triggered. `mouseout` fires when the pointer moves out of child element as well, while `mouseleave` fires only when the pointer moves out of the bound element. | | | | --- | --- | | 1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29<br>30<br>31<br>32<br>33<br>34<br>35<br>36<br>37<br>38<br>39<br>40<br>41<br>42<br>43<br>44<br>45<br>46<br>47<br>48<br>49<br>50<br>51<br>52<br>53<br>54<br>55<br>56<br>57<br>58<br>59<br>60<br>61<br>62<br>63<br>64 | ```<br><!doctype html><br><html lang="en"><br><head><br> <meta charset="utf-8"><br> <title>on demo</title><br> <style><br> div.out {<br> width: 40%;<br> height: 120px;<br> margin: 0 15px;<br> background-color: #d6edfc;<br> float: left;<br> }<br> div.in {<br> width: 60%;<br> height: 60%;<br> background-color: #fc0;<br> margin: 10px auto;<br> }<br> p {<br> line-height: 1em;<br> margin: 0;<br> padding: 0;<br> }<br> </style><br> <script src="https://code.jquery.com/jquery-3.7.1.js"></script><br></head><br><body><br> <br><div class="out overout"><br> <p>move your mouse</p><br> <div class="in overout"><p>move your mouse</p><p>0</p></div><br> <p>0</p><br></div><br><div class="out enterleave"><br> <p>move your mouse</p><br> <div class="in enterleave"><p>move your mouse</p><p>0</p></div><br> <p>0</p><br></div><br> <br><script><br>var i = 0;<br>$( "div.overout" )<br> .on( "mouseover", function() {<br> $( "p", this ).first().text( "mouse over" );<br> } )<br> .on( "mouseout", function() {<br> $( "p", this ).first().text( "mouse out" );<br> $( "p", this ).last().text( ++i );<br> } );<br> <br>var n = 0;<br>$( "div.enterleave" )<br> .on( "mouseenter", function() {<br> $( "p", this ).first().text( "mouse enter" );<br> } )<br> .on( "mouseleave", function() {<br> $( "p", this ).first().text( "mouse leave" );<br> $( "p", this ).last().text( ++n );<br> } );<br></script><br> <br></body><br></html><br>```