How to select jQuery child with one selector?

I have divwith id article_50. If I had to select an item .titleinside article_50, I could do the following: $("#article_50 .title").

Now say that I want to select the same header, but inside the click event:

$("#article_50").click(function(){
    $(this).children(".title").hide();
});

Now I had to call a method childrento select this item .title. Can I select it in one selector without calling children?

Something like that:

$("this .title").hide();

Thank!

+3
source share
4 answers

Almost there:

$(".title", this).hide();
+8
source
$("#article_50").click(function(){ $(".title", this).hide(); });
+1
source

.children(), .title this.

:

$(".title", this).hide();

... jQuery 7 8 , , .title this.

jQuery :

$(this).find('.title')

... . . , .

, .children() , .children(), .find(), .


EDIT:

- #article_50 , jQuery $(this) , .

var $art_50 = $("#article_50").click(function(){
    $art_50.children(".title").hide();
});

// Now you have a variable that you can use outside the click handler as well
//   in case you are selecting it elsewhere in your code.
+1

:

$("#article_50").click(function(){ $("#article_50 .title").hide(); });

, .

0

Source: https://habr.com/ru/post/1761961/


All Articles