Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have four li elements, I want to show div and hide it on hover of two li elements only.
here is my code
HTML
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}

li {
    display: inline;
}
#first:hover
{
background-color: red;
}
#second:hover
{
background-color: red;
}

</style>

<script type="text/javascript">
$('#first li').hover(function(){
     $('.div1', this).show();  //find the div INSIDE this li
},function(){
     $('.div1', this).hide();
});;

</script>
</head>
<body>

<ul>
  <li><a href="#home">Home</a></li>
  <li id="first"><a href="#news">News</a></li>
  <li id="second"><a href="#contact">Contact</a></li>
  <li><a href="#about">About</a></li>
</ul>
<div class="div1"   style="Width:200px;height:150px;border:2px solid black;display:none;">asd</div>
<div class="div2" style="Width:200px;height:50px;border:2px solid black;display:none;">sgtargewarasd</div>
</body>
</html>
Posted

Use mouseenter and leave instead of hover.

JavaScript
<script type="text/javascript">

   $('#first').mouseenter(function(){
     $('.div1').show(); 
  });
  $('#first').mouseleave(function(){
     $('.div1').hide(); 
  });


  $('#second').mouseenter(function(){
     $('.div2').show(); 
  });
  $('#second').mouseleave(function(){
     $('.div2').hide(); 
  });

</script>


An also find my fiddle for above solution: fiddle[^]
 
Share this answer
 
Comments
Hussain Javed 21-Nov-15 5:14am    
I have tried this but its not working
here is my code
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
ul {
list-style-type: none;
margin: 0;
padding: 0;
}

li {
display: inline;
}
.first:hover
{
background-color: red;
}
.second:hover
{
background-color: red;
}



</style>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.7.2.js"></script>
<script type="text/javascript">

$('#first').mouseenter(function(){
$('.div1').show();
});
$('#first').mouseleave(function(){
$('.div1').hide();
});

$('#second').mouseenter(function(){
$('.div2').show();
});
$('#second').mouseleave(function(){
$('.div2').hide();
});

</script>
</head>
<body>

<ul>
<li>Home</li>
<li id="first">News</li>
<li id="second">Contact</li>
<li>About</li>
</ul>
<div class="div1" style="Width:200px;height:150px;border:2px solid black;display:none;">asd</div>
<div class="div2" style="Width:200px;height:50px;border:2px solid black;display:none;">sgtargewarasd</div>
</body>
</html>
Hussain Javed 21-Nov-15 5:35am    
If i hover on any li element the div contains another list of items,if mouseleave that element then that li element should not hidden.
If i hover another li element then first li element div should be hidden and second li element should be show.
for example you can see www.cloudingsystem.com
Sarath kumar.N 22-Nov-15 23:08pm    
HI Javed I tried ur code it's working fine. I tried in jsfiddle. I suggest u to use developer tools in your browser to debug. Please check whether any error in ur code or jquery loaded fine. Thanks.
You can try this way

JavaScript
$(function() {
    
  <script type="text/javascript">
    $(function() {
    
$('li#first  ').hover(function(e){

     $('.div1').show();  //find the div INSIDE this li
},function(e){
 
 

if(e.relatedTarget.className !='div1')
{
     $('.div1').hide();
     }
});
 });
    </script>;
 
Share this answer
 
v2
Comments
Hussain Javed 21-Nov-15 5:35am    
ok i have tried your function.Its Working.
If i hover on any li element the div contains another list of items,if mouseleave that element then that li element should not hidden.
If i hover another li element then first li element div should be hidden and second li element should be show.
for example you can see www.cloudingsystem.com
Anil Sharma1983 21-Nov-15 6:50am    
please check updated soultion

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900