Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery getting child div within a div

Tags:

jquery

I have a css div and within that div another div exists. How can I get the child div while the mouse hovers over the parent div? As here:

<div class="parent">
  <div class="child">
  </div>
</div>

When the mouse gets over the parent div, I want the child div to be displayed.

like image 490
Shahid Karimi Avatar asked Mar 11 '11 12:03

Shahid Karimi


People also ask

How to get div child div in jQuery?

children() is an inbuilt method in jQuery which is used to find all the children element related to that selected element. This children() method in jQuery traverse down to a single level of the selected element and return all elements.

How can I get second child in jQuery?

grab the second child: $(t). children(). eq(1);

What is parent and child in jQuery?

The parent > child selector in jQuery is used to select all elements that are a direct child of the specified element.


1 Answers

This works:

$('.child').hide();   // or $('.parent').children().hide();

$('.parent').hover(
    function() { $(this).children('div').show() },
    function() { $(this).children('div').hide() }
);

Example at http://jsfiddle.net/4kMuD/, although that uses IDs rather than classes for the selector.

I've assumed (although you didn't say) that you want the child div to disappear again when you're no longer hovering over the parent.

like image 88
Alnitak Avatar answered Oct 03 '22 15:10

Alnitak