dvln
dvln

Reputation: 121

jquery show/hide make element visible on hover

I have created a simple functionality using jquery to show and hide a div on hover of an anchor. But, as soon as I leave the anchor tag, the div disappears. I'm unable to go through the div. I should be able to stay on the div element. please can any one help on this.

Here is the fiddle

http://jsfiddle.net/mvqxy9pb/

<style>
.container {
width: 200px;
position: relative;
display: inline-block;
}


.dropdown{
position:absolute;
left:0px;
width:250px;
top:18px;
background-color:#c03;
height:100px;
display:none;
}

</style>


<script>
$(document).ready(function(){
$(".menuItem").hover(function(){
$(".dropdown").show();
}, function(){
$(".dropdown").hide();
});
});
</script>


<div class="container">


<div class="wrap">

<a href="#" class="menuItem">Menu Item</a>

<div class="dropdown">
asasasasa
</div>

</div>


</div> 

Upvotes: 0

Views: 1624

Answers (3)

quead
quead

Reputation: 178

You can try also using css instead of jquery

http://jsfiddle.net/mvqxy9pb/3/

I've added menuItem as primary div to (anchor and dropdown)

<div class="menuItem">
   <a href="#">Menu Item</a>
   <div class="dropdown">
       asasasasa
   </div>
</div>

And i've removed the position:relative; from container and added to menuItem then added on menuItem:hover added display:block; for showing the dropdown.

.menuItem:hover .dropdown {
    display:block !important;
}

Upvotes: 1

Vasiliy vvscode Vanchuk
Vasiliy vvscode Vanchuk

Reputation: 7159

Try

$(document).ready(function(){
  $(".menuItem, .dropdown").hover(function(){
   $(".dropdown").show();
  }, function(){
   $(".dropdown").hide();
  });
});

https://jsfiddle.net/mvqxy9pb/

Upvotes: 0

SO-user
SO-user

Reputation: 1456

Change the below line

$(".menuItem").hover(function(){

to this

$(".menuItem, .dropdown").hover(function(){

Demo: http://jsfiddle.net/mvqxy9pb/1/

Upvotes: 4

Related Questions