Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JQuery to show a div's child elements only?

So I have this jquery function that's supposed to show a class's hidden span on hover. How do i set up the function so it only shows the selected div's child span (instead of showing all the spans on the page)?

Here's my jquery function:

$(".thumb").hover(
    function() {
       $(".blurb").show();
    },
    function(){
       $(".blurb").hide();
    }
);

You can view the jsfidde here. Thanks!

like image 968
raeq Avatar asked Mar 09 '11 19:03

raeq


2 Answers

That's what this is for!

$(".thumb").hover(
    function() {
       $(this).children('.blurb').show();
    },
    function(){
       $(this).children('.blurb').hide();
    }
);
like image 63
Intelekshual Avatar answered Oct 17 '22 17:10

Intelekshual


Use $(this).children() instead of executing a global query again:

$(".thumb").hover(function() {
    $(this).children().show();
}, function() {
    $(this).children().hide();
});

Working demo: http://jsfiddle.net/h5x3f/2/

Note: if you're not bothered about supporting Internet Explorer 6, you can avoid jQuery/JavaScript completely and use CSS's :hover pseudo-class, which will even work with JS disabled. Or you could use a shim like ie-7.js to handle :hover for you. See this variation of your fiddle for an example.

like image 20
Andy E Avatar answered Oct 17 '22 15:10

Andy E