Mike Darling
Mike Darling

Reputation: 177

Jquery select children without class like

I know I'm close, but need a hint here! I'm trying to select data from certain class elements contained insider other elements based on whether a specific parent has a tag in the class name or not.

<div class=something_1>
<div class=other>
<div class=other>
<div class=other_TEMPLATE_1>
<div class=other>
<div class=selected_thing>I DO NOT WANT THIS</div>
</div>
</div>
</div>
</div>
</div>

<div class=something_2>
<div class=other>
<div class=other>
<div class=other_TEMPLATE_2>
<div class=other>
<div class=selected_thing>I DO NOT WANT THIS</div>
</div>
</div>
</div>
</div>
</div>

<div class=something_3>
<div class=other>
<div class=other>
<div class=other>
<div class=other>
<div class=selected_thing>I WANT THIS</div>
</div>
</div>
</div>
</div>
</div>

<script>

$("[class^=something]").each(function(){

selectedThing = $(this).children().children().children(":not[class^=other_TEMPLATE]").children().children(".selected_thing").html();

console.log(selectedThing);

});

</script>

As you can see part of the challenge is that some of the class names can change slightly but I'm wanting to ignore those where TEMPLATE shows up.

Upvotes: 0

Views: 3951

Answers (4)

Sam R
Sam R

Reputation: 702

$('.selected_thing').not('[class*="TEMPLATE"] .selected_thing');

Accompanying JSfiddle

Upvotes: 1

JOSEFtw
JOSEFtw

Reputation: 10071

var selectedThings = [];

$("[class^=something]").each(function(){

selected = $(this).find('div')
.not("[class^=other_TEMPLATE]")
.children().children(".selected_thing")
.html();

if(selected !== undefined)
{
    selectedThings.push(selected);
}

});

console.log(selectedThings);

EDIT: To slow :( @nevermind won...

Upvotes: 1

j08691
j08691

Reputation: 207881

You could use this:

$('.selected_thing').each(function(){
    if($(this).parents('[class*="TEMPLATE"]').length==0){
    //do something
        $(this).css('color','red')
    }
})

jsFiddle example

Upvotes: 2

sinisake
sinisake

Reputation: 11318

$("[class^=something]").each(function(){

selectedThing = $(this).find('div').not("[class^=other_TEMPLATE]").children().children(".selected_thing").html();

console.log(selectedThing);

});

Upvotes: 1

Related Questions