How to select children elements but only one level deep with jQuery

Find the immediate grand-children:

​$("#div1").children().children(".b");

Fiddle: http://jsfiddle.net/jonathansampson/Dy6GJ/

If you don't know how deep to go, but want all .b not within a .b, use a filter while respecting parent limitations. You could use the .parentsUntil method:

var parent = "#div1";
$(".b", parent).filter(function(){
    return !$(this).parentsUntil(parent, ".b").length;
}).css("border", "1px solid red");​​​​​​​​​​​​​​​​​

Fiddle: http://jsfiddle.net/jonathansampson/Dy6GJ/3/


General (when only class can be guaranteed):

$("#div1").find(".b:not(.b .b)")

As described (likely faster than general, though not tested):

$("#div1").find("div.b:not(div.b div.b)")

"All .b which are not descendants of .b"

Tags:

Jquery