April 1, 201610 yr Yes: http://api.jquery.com/hover/ But why would you want to when CSS does it so much better?
April 1, 201610 yr Inline: <h1 id="myHeading" onmouseover="console.log('hover event detected')">Hover over this</h1> In a script block "classic" syntax with anonymous handler function document.getElementById('myHeading').onmouseover = function(){console.log('hover event detected')}; In a script block "modern" syntax with named event handler function (can be called by other events as well) function myFunction () { console.log('hover event detected'); } document.getElementById('myHeading').addEventListener('mouseover', myFunction); Edited April 1, 201610 yr by Nillervision
April 1, 201610 yr Yes: http://api.jquery.com/hover/ But why would you want to when CSS does it so much better? That's jQuery, a library sat on JavaScript not the actual language, it may be overkill depending on the project the OP is creating. In many cases I advocate it's use, but we shouldn't make a suggestion for or against when we know nothing of the requirements. CSS being better than JavaScript for hover events is entirely subjective. CSS hover events are only able to change visual style, you cannot maintain that state when the mouse leaves the element or fire other events or function calls etc. With JavaScript's hover event you can do whatever you please. Whether or not you use CSS or JavaScript for an event like hover is all dependant on what result or feedback you wish to deliver to the end user. Edited April 1, 201610 yr by rbrtsmith
April 1, 201610 yr Inline: <h1 id="myHeading" onmouseover="console.log('hover event detected')">Hover over this</h1> In a script block "classic" syntax with anonymous handler function document.getElementById('myHeading').onmouseover = function(){console.log('hover event detected')}; In a script block "modern" syntax with named event handler function (can be called by other events as well) function myFunction () { console.log('hover event detected'); } document.getElementById('myHeading').addEventListener('mouseover', myFunction); I think it's worth explaining why the third one you suggested is the better of the three To the OP don't do the first!
April 1, 201610 yr I think it's worth explaining why the third one you suggested is the better of the three To the OP don't do the first! OK I will try my best (but you could probably explain it better ) The reason to avoid inline event attributes (especially with script statements rather than function calls) is first and foremost seperation of concerns and abstraction. If you develop anything slightly complex you would want to be able to reuse your eventlisteners (and handler functions). By seperating your JS from your mark-up you can also keep it all in a single separate file that you can link to from all your pages. That way you will only have to update the code one place if you decide to change a function. Regarding what type of eventlistener to use: .addEventListener is more flexible than .onouseover first of all because there is no limmit to the number of listeners you can attach to an element. With onclick, onmouseover etc. you can not attach new listeners without overwriting any existing listeners (of the same type). Also addEventListener has a third (optional) parameter (for mouse and touch events) in which you can invert the standard capturing of events (useful for changing the order events on nested elements should be fired) I did not cover this parameter in my first answer because in most cases you would want the default behavior and because it is difficult to explain without visual examples. But there is a tutorial here: http://javascript.info/tutorial/bubbling-and-capturing I should also mention that .addEventListener is not supported in older versions of IE which really would be the only argument to use onmouseover instead Edited April 1, 201610 yr by Nillervision
April 1, 201610 yr Three examples: With JQuery:http://stackoverflow.com/questions/3...r-using-jqueryWith CSS:http://jsfiddle.net/gd8ba/light/ Directly in HTML with a little bit of JavaScript:https://www.interspire.com/support/k...+An+Image+Swap
April 1, 201610 yr Three examples: With JQuery: http://stackoverflow.com/questions/3...r-using-jquery With CSS: http://jsfiddle.net/gd8ba/light/ Directly in HTML with a little bit of JavaScript: https://www.interspire.com/support/k...+An+Image+Swap Not the best examples jQuery suggestion: OP asked for a JavaScript solution and no frameworks was mentioned so we should stick to Vanilla. Beside loading a full library to add a few hover events seems like overkill as @@rbrtsmith has already mentioned. CSS suggestion: Exactly what OP did NOT ask for onmouseover attribute suggestion: I have already explained this and why it should be avoided. And the article you link to uses bad practices: onMouseOver in camelcase isn't the correct naming convention. It will be interpreted OK as an attribute, but if people pick up the habit they will get an error when trying to attach the eventlistener in JavaScript (calls to js methods and functions are case sensitive). Don't take this the wrong way @@Bitpalace Your intentions to help are good but on some of your post it looks like you don't take the time to read the whole thread before you answer. Edited April 1, 201610 yr by Nillervision
April 2, 201610 yr There's also mouseenter which behaves a little differently to mouseover.The mouseover event bubbles so it will be triggered when entering the element and any descendants (if hovered over) which may lead to unexpected results depending on the function being called.The mouseenter event will only be triggered once when entering the element no matter what descendants the mouse also enters.Here's a simple demo, but MDN may explain it better.jQuery uses mouseenter for it's hover function.
Create an account or sign in to comment