1

All I want to do is hide the class "col-a" and show "col-b" using jquery hover method but for some reason jquery is giving me some funky results.

here is the snippet

$(".col-a").hover(function(){
    $(this).hide().next().show(1000)
    }, function(){
    $(".col-b").hide().prev().show(1000);
});
.hide {
    display:none;
}
.col-a {
    height:200px;
    margin:10px;
    float:left;
    width:200px;
    background-color:yellow;
}
.col-b {
    height:200px;
    margin:10px;
    display:none;
    float:left;
    width:200px;
    background-color:yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="col-a">One</div>
<div class="col-b">Two</div>
<div class="col-a">One</div>
<div class="col-b">Two</div>

Here is a fiddle:

fiddle

2
  • Why are there 2 divs with the same class next to eachother? Commented Feb 20, 2015 at 19:01
  • Like this jsfiddle.net/21fn2zg6/2? Commented Feb 20, 2015 at 19:05

3 Answers 3

2

Hi you can use instead of hover() event just the mouseover for col-a and then the mouseout for col-b. Try this:

$(".col-a").mouseover(function(){
    $(this).hide();
    $(this).next(".col-b").fadeIn('100');
});
$(".col-b").mouseout(function(){
    $(this).hide();
    $(this).prev(".col-a").fadeIn('100');
});
.hide {
    display:none;
}
.col-a {
    height:200px;
    margin:10px;
    float:left;
    width:200px;
    background-color:yellow;
}
.col-b {
    height:200px;
    margin:10px;
    display:none;
    float:left;
    width:200px;
    background-color:yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="col-a">One</div>
<div class="col-b">Two</div>
<div class="col-a">One</div>
<div class="col-b">Two</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. Should I just avoid the hover method all together when working with block elements?
@user1119742 np glad to help you .... The block elements doesn't mean avoide the hover is just this specific situation, evaluate your actual behavior and wich event you need to trigger. When a is gone there is no more hover and generate the flicked effect so we need just the enter event then appears b and we just need the out.
0
<div class="col-a">One</div>
<div class="col-b">Two</div>  



$(document).ready(function(){
    $(".col-a").hover(
       function(){
                 $(this).hide();
                 $('.col-b').show();
      },
        function(){
                 $(this).show(1000);
                 $('.col-b').hide();
     });
    });

Although you can get your result using mouseover and mouseout on .col-a and .col-b respectively

Comments

-1

Hi you can use this.

<script>
 $(".col-b").fadeOut("slow)
</Script>

1 Comment

It's a good idea to test your code before you post it - I've seen you post erroneous code twice today. You're missing a quote. It also doesn't answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.