JQuery selector help

Using jQuery, how do I select all elements with class x inside an element with id y?

+3
source share
5 answers

Select all descendants with class x of the element with id "y".

$("#y .x").each(function () {
   $(this) <- your element
});

Selects all children with the class x of the element with the identifier "y".

$("#y > .x").each(function () {
   $(this) <- your element
});
+6
source

$('#y .x') should do it for you.

note that this will select all descendants with class x, not just children.

+5
source
$("#x .y").doSomething();

$(".y", "#x").doSomething();

$("#x").find(".y").doSomething();

:

$("#x > .y").doSomething();

$("#x").children(".y").doSomething();

, , . - jQuery?

+4

$("#id .class")

+2

If you have element 1 with id = 'y' and you want all its [immediate] children to have class = 'x'

$("#y > .x").each(function(){stuff]);

If you want all decoders id = 'y' (and not just right away), you should:

$("#y").find(".x").each(function(){stuff});

Obviously, you could make it smarter (and better) by adding element types if you know what they are. For example, if you want only children of the type, then:

$("#y > a.x").each(function(){stuff]);

Hope you have in mind.

+1
source

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


All Articles