I need to add css class .hovered
on html element when hover, for this I created a small example snippet below. It has couple of issues.
When I hover on a child element it shouldn’t add .hovered
class to its parents, for example in below snippet, a
inside nav
element, it shouldn’t add border to it’s parent (nav
).
But most importantly, I need to select elements in DOM dynamically, so instead this $( "div,nav,h1,h5,p,a,button")
I would like to provide something like $( "*")
, so I would be able to hover on any kind of element.
$(document).ready(function(){
$( "div,nav,h1,h5,p,a,button")
.mouseenter(function() {
$( this ).addClass("hovered");
})
.mouseleave(function() {
$( this ).removeClass("hovered");
});
});
.hovered {
border: 2px solid red;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-white border-bottom shadow-sm">
<h5 class="my-0 mr-md-auto font-weight-normal">Company name</h5>
<nav class="my-2 my-md-0 mr-md-3">
<a class="p-2 text-dark" href="#">Features</a>
<a class="p-2 text-dark" href="#">Enterprise</a>
<a class="p-2 text-dark" href="#">Support</a>
<a class="p-2 text-dark" href="#">Pricing</a>
</nav>
<button>Sign up</button>
</div>
<div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center">
<h1 class="display-4">Title</h1>
<p class="lead">paragrpah</p>
</div>
How could I achieve it with vanilla JavaScript or jQuery?
3
Answers
I’m not sure how efficient doing what you want can be, as you want to run this for every element on every mouse action. But if you really want to, you can do it like this:
How this works:
body
andhtml
Remember that when an element is hovered, it’s parent (and its parent’s parent etc) are also hovered, so this is why we need to remove the class from the parent.
Working Example:
Update: The 2 issues you mention are to do with your HTML. You can fix them as follows:
Your
nav
element isn’t fully containing your menu items – they are extending outside the nav boundary because you addedp-2
to them. Addingp-2
to the nav will make the hover work as expected.White space doesn’t trigger a hover event, and your
a
elements are separated with white space (a linebreak, tab or space will do this). You can delete the white space from between thea
elements like this – it’s a bit of a hack but it works:These are fixed in the working example above if you try it now
In Javascript you could do something like below:
You can get element by there tagName’s(like div, h1) and get them by getElementsByTagName or you can add classes to these element and get by getElementsByClassName
You probably don’t need Js here.
CSS-only solution: