Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery; Check if object has child $(this)

I wonder how I in the best way can see if a container div contains a child element. I have a click event that fires on a child of either div id=unread or div id=read. I want to check where this child is.

Something like this is what I'm thinking:

if ($("#unread").find($(this)))
    alert("unread");
else
    alert("read");

Edit: $(this) is a descendants two levels from #unread or #read.

Regards

like image 399
user822448 Avatar asked Jun 30 '11 06:06

user822448


1 Answers

Make use of : .children()

if( $("#unread").children().length > 0)
    alert("unread");
else
    alert("read");

EDIT

if($(event.target).closest('#unread').length > 0)
    alert('unread');
else
    alert('read');
like image 157
Pranay Rana Avatar answered Nov 15 '22 14:11

Pranay Rana