jQuery find only not nested elements -


given html structure

<div class="item">     <div class="item">         select me         <div class="item">don't select me</div>     </div>     <span>         <div class="item">select me</div>     </span> </div> 

and jquery:

var firstitem = $('.item:first'); var selector = firstitem.find('.item'); selector.css('color', 'red'); 

i looking way mark not nested .item elements inside firstitem div. tricky part here firstitem (or can be) nested in .item class.


what have tried?

firstitem.find('.item').not('.item .item') 

this not work because first level starting in .item class. , can nested x times .item .item .item .item or whatever. , starting element not have id cannot use in selector well.


i think solution should reversed .closest() travels down dom , not search contents of found item.

prepared jsfiddle here: http://jsfiddle.net/lwmy3/2/

try

var firstitem = $('.item:first'); $(firstitem).find(".item").each(function () {     //console.log($(this).parent(".item")[0])     if ($(this).parent(".item")[0] === undefined) {         $(this).css('color', 'red')     } else if ($(this).parent(".item")[0] != $(firstitem)[0]) {         $(this).css('color', 'cyan')     } else {         $(this).css('color', 'red')     } }); 

fiddle


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -