jqueryparent

jquery this child selector?


Quick question, I couldn't find anything on the web. I have a parent div and a child div inside of it. to select it with css you would say:

#parent .child {}

In jQuery, I have a variable for my parent element. However, how can I select the child with this? I know it's easy to create a new variable, I'm just curious if it's possible?

const parent = $('#parent');
    
parent.click(function() {
  $(this > '.child').hide();
})

thank you


Solution

  • The correct syntax is:

    $(".child", this)
    

    If you only want the direct children:

    $("> .child", this)
    

    (credit goes to Gumbo for mentioning this)

    Update, two years later:

    You can use $(this).find('> .child')