Skip to content Skip to sidebar Skip to footer

Selecting Only One Div Of Entire Css Class In JQuery

my goal is to show an overlay on a div when that div is hovered on. The normal div is called .circleBase.type1 and the overlay is circleBase.overlay. I have multiple of these divs

Solution 1:

Use $(this) to get current element reference and do like this:

$(".circleBase.type1").mouseenter(function(){
    $(this).next(".overlay").fadeIn("fast");
    $(this).next(".overlay").find('.date').show();
    $(this).find('.hidetext').hide();
});

and:

$(".overlay").mouseleave(function(){
        $(this).fadeOut("fast");
        $(this).find('.date').hide();
        $(this).prev(".circleBase").find('.hidetext').show();
    });

Solution 2:

usually when I want to target something specific you just give it an ID.

ID's play better in JavaScript than classes.

If you had a specific container, using the container as your starting point is a good route as well

$('#container').find('.something.type1').doSomething();

This is much more efficient for jquery, because it only searches .something.type1 inside of #container.


Solution 3:

Well I'm not sure exactly what you're looking to do, but it looks like you want to replace content in some kind of circle with a hover text, but with a fade. To do that you'll have to add some CSS and it would be best to change your HTML structure too.

The HTML should look like this:

<div class="circleContainer">
    <div class="circleBase">
        <p>Lorem ipsum</p>
        <hr>
        <strong class="gray">gdroel</strong>
    </div>
    <div class="overlay" style="display: none;">
       <p class="date">11/12/14</p>
    </div>
</div>

so your js can look like this:

$(function(){
    $(".circleContainer").mouseenter(function(){
        $(this).find(".overlay")
        $(this).find('.circleBase').hide();
    });

    $(".circleContainer").mouseleave(function(){
        $(this).find('.circleBase').show();
        $(this).find(".overlay").hide();
    });
});

Here's a working solution that includes some CSS to make it nice. Try taking it out and running it, you'll see the problems right away.


Post a Comment for "Selecting Only One Div Of Entire Css Class In JQuery"