CSS/jQuery selector to find elements which don't have a certain parent












2















I found solutions to check if the parent does not have a certain class using :parent, but I want to have a node type excluded. In my case I only wants forms which are not placed withing an article.



I found a solution with filter, but I was wondering if there is a selector only way to achieve the same:



$('form.frm').filter(function() {
return $(this).parent().is(":not(article)");
});


EDIT: This solution isn't perfect as it only works if the form.frm is an immediate child of article. Sometimes I have a div in between.
Here a simplified DOM structure:



<div id="pageContent">
<articel>
<div class="box">
<form class="frm"></form>
</div>
</article>
<articel>
<form class="frm"></form>
</article>
</div>









share|improve this question




















  • 1





    Whats the parent of article element? Would be great if you can share relevant DOM

    – Milind Anantwar
    Nov 22 '18 at 14:08











  • You can produce a list of all elements that are not a child of an article. querySelectorAll(':not(article)>*') but you really mean a list of all elements that are not a descendant of an article, right?

    – Mr Lister
    Nov 22 '18 at 14:10


















2















I found solutions to check if the parent does not have a certain class using :parent, but I want to have a node type excluded. In my case I only wants forms which are not placed withing an article.



I found a solution with filter, but I was wondering if there is a selector only way to achieve the same:



$('form.frm').filter(function() {
return $(this).parent().is(":not(article)");
});


EDIT: This solution isn't perfect as it only works if the form.frm is an immediate child of article. Sometimes I have a div in between.
Here a simplified DOM structure:



<div id="pageContent">
<articel>
<div class="box">
<form class="frm"></form>
</div>
</article>
<articel>
<form class="frm"></form>
</article>
</div>









share|improve this question




















  • 1





    Whats the parent of article element? Would be great if you can share relevant DOM

    – Milind Anantwar
    Nov 22 '18 at 14:08











  • You can produce a list of all elements that are not a child of an article. querySelectorAll(':not(article)>*') but you really mean a list of all elements that are not a descendant of an article, right?

    – Mr Lister
    Nov 22 '18 at 14:10
















2












2








2


0






I found solutions to check if the parent does not have a certain class using :parent, but I want to have a node type excluded. In my case I only wants forms which are not placed withing an article.



I found a solution with filter, but I was wondering if there is a selector only way to achieve the same:



$('form.frm').filter(function() {
return $(this).parent().is(":not(article)");
});


EDIT: This solution isn't perfect as it only works if the form.frm is an immediate child of article. Sometimes I have a div in between.
Here a simplified DOM structure:



<div id="pageContent">
<articel>
<div class="box">
<form class="frm"></form>
</div>
</article>
<articel>
<form class="frm"></form>
</article>
</div>









share|improve this question
















I found solutions to check if the parent does not have a certain class using :parent, but I want to have a node type excluded. In my case I only wants forms which are not placed withing an article.



I found a solution with filter, but I was wondering if there is a selector only way to achieve the same:



$('form.frm').filter(function() {
return $(this).parent().is(":not(article)");
});


EDIT: This solution isn't perfect as it only works if the form.frm is an immediate child of article. Sometimes I have a div in between.
Here a simplified DOM structure:



<div id="pageContent">
<articel>
<div class="box">
<form class="frm"></form>
</div>
</article>
<articel>
<form class="frm"></form>
</article>
</div>






javascript jquery html jquery-selectors






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 14:43









Mohammad

15.7k123563




15.7k123563










asked Nov 22 '18 at 13:57









ThomasThomas

1,676526




1,676526








  • 1





    Whats the parent of article element? Would be great if you can share relevant DOM

    – Milind Anantwar
    Nov 22 '18 at 14:08











  • You can produce a list of all elements that are not a child of an article. querySelectorAll(':not(article)>*') but you really mean a list of all elements that are not a descendant of an article, right?

    – Mr Lister
    Nov 22 '18 at 14:10
















  • 1





    Whats the parent of article element? Would be great if you can share relevant DOM

    – Milind Anantwar
    Nov 22 '18 at 14:08











  • You can produce a list of all elements that are not a child of an article. querySelectorAll(':not(article)>*') but you really mean a list of all elements that are not a descendant of an article, right?

    – Mr Lister
    Nov 22 '18 at 14:10










1




1





Whats the parent of article element? Would be great if you can share relevant DOM

– Milind Anantwar
Nov 22 '18 at 14:08





Whats the parent of article element? Would be great if you can share relevant DOM

– Milind Anantwar
Nov 22 '18 at 14:08













You can produce a list of all elements that are not a child of an article. querySelectorAll(':not(article)>*') but you really mean a list of all elements that are not a descendant of an article, right?

– Mr Lister
Nov 22 '18 at 14:10







You can produce a list of all elements that are not a child of an article. querySelectorAll(':not(article)>*') but you really mean a list of all elements that are not a descendant of an article, right?

– Mr Lister
Nov 22 '18 at 14:10














2 Answers
2






active

oldest

votes


















2














Answer is a modification of Mohammad's. The difference is that this code is not using filter, it is using not:



$('form.frm').not('article form.frm')


This is more or less the same thing. You specify a global selector, then exclude the forms you do not want (in this case, any form that is a descendent of article).






$('form.frm').not('article form.frm').css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>








share|improve this answer
























  • Thanks, now the question, which solution is faster. Using the .not function or the filter with the .closest. I would assume the former...

    – Thomas
    Nov 22 '18 at 14:42





















2














Use .closest() to find any article parent of element and check if length of selector is 0.



$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');





$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>





Also you can select all .frm and use .not() to exclude all element has article parent.



$('.frm').not($('article').find('.frm')).css('color', 'red');





share|improve this answer


























  • I just found this solution as well, but still wondering if there is a way to achieve this without filter

    – Thomas
    Nov 22 '18 at 14:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432570%2fcss-jquery-selector-to-find-elements-which-dont-have-a-certain-parent%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Answer is a modification of Mohammad's. The difference is that this code is not using filter, it is using not:



$('form.frm').not('article form.frm')


This is more or less the same thing. You specify a global selector, then exclude the forms you do not want (in this case, any form that is a descendent of article).






$('form.frm').not('article form.frm').css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>








share|improve this answer
























  • Thanks, now the question, which solution is faster. Using the .not function or the filter with the .closest. I would assume the former...

    – Thomas
    Nov 22 '18 at 14:42


















2














Answer is a modification of Mohammad's. The difference is that this code is not using filter, it is using not:



$('form.frm').not('article form.frm')


This is more or less the same thing. You specify a global selector, then exclude the forms you do not want (in this case, any form that is a descendent of article).






$('form.frm').not('article form.frm').css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>








share|improve this answer
























  • Thanks, now the question, which solution is faster. Using the .not function or the filter with the .closest. I would assume the former...

    – Thomas
    Nov 22 '18 at 14:42
















2












2








2







Answer is a modification of Mohammad's. The difference is that this code is not using filter, it is using not:



$('form.frm').not('article form.frm')


This is more or less the same thing. You specify a global selector, then exclude the forms you do not want (in this case, any form that is a descendent of article).






$('form.frm').not('article form.frm').css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>








share|improve this answer













Answer is a modification of Mohammad's. The difference is that this code is not using filter, it is using not:



$('form.frm').not('article form.frm')


This is more or less the same thing. You specify a global selector, then exclude the forms you do not want (in this case, any form that is a descendent of article).






$('form.frm').not('article form.frm').css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>








$('form.frm').not('article form.frm').css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>





$('form.frm').not('article form.frm').css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 14:37









Richard Parnaby-KingRichard Parnaby-King

9,751857111




9,751857111













  • Thanks, now the question, which solution is faster. Using the .not function or the filter with the .closest. I would assume the former...

    – Thomas
    Nov 22 '18 at 14:42





















  • Thanks, now the question, which solution is faster. Using the .not function or the filter with the .closest. I would assume the former...

    – Thomas
    Nov 22 '18 at 14:42



















Thanks, now the question, which solution is faster. Using the .not function or the filter with the .closest. I would assume the former...

– Thomas
Nov 22 '18 at 14:42







Thanks, now the question, which solution is faster. Using the .not function or the filter with the .closest. I would assume the former...

– Thomas
Nov 22 '18 at 14:42















2














Use .closest() to find any article parent of element and check if length of selector is 0.



$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');





$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>





Also you can select all .frm and use .not() to exclude all element has article parent.



$('.frm').not($('article').find('.frm')).css('color', 'red');





share|improve this answer


























  • I just found this solution as well, but still wondering if there is a way to achieve this without filter

    – Thomas
    Nov 22 '18 at 14:33
















2














Use .closest() to find any article parent of element and check if length of selector is 0.



$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');





$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>





Also you can select all .frm and use .not() to exclude all element has article parent.



$('.frm').not($('article').find('.frm')).css('color', 'red');





share|improve this answer


























  • I just found this solution as well, but still wondering if there is a way to achieve this without filter

    – Thomas
    Nov 22 '18 at 14:33














2












2








2







Use .closest() to find any article parent of element and check if length of selector is 0.



$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');





$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>





Also you can select all .frm and use .not() to exclude all element has article parent.



$('.frm').not($('article').find('.frm')).css('color', 'red');





share|improve this answer















Use .closest() to find any article parent of element and check if length of selector is 0.



$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');





$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>





Also you can select all .frm and use .not() to exclude all element has article parent.



$('.frm').not($('article').find('.frm')).css('color', 'red');





$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>





$('form.frm').filter(function() {
return $(this).closest('article').length == 0;
}).css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageContent">
<article>
<div class="box">
<form class="frm">article > div</form>
</div>
</article>
<article>
<form class="frm">article</form>
</article>
<div class="box">
<form class="frm">div</form>
</div>
</div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 14:37

























answered Nov 22 '18 at 14:32









MohammadMohammad

15.7k123563




15.7k123563













  • I just found this solution as well, but still wondering if there is a way to achieve this without filter

    – Thomas
    Nov 22 '18 at 14:33



















  • I just found this solution as well, but still wondering if there is a way to achieve this without filter

    – Thomas
    Nov 22 '18 at 14:33

















I just found this solution as well, but still wondering if there is a way to achieve this without filter

– Thomas
Nov 22 '18 at 14:33





I just found this solution as well, but still wondering if there is a way to achieve this without filter

– Thomas
Nov 22 '18 at 14:33


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432570%2fcss-jquery-selector-to-find-elements-which-dont-have-a-certain-parent%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?