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?