CSS/jQuery selector to find elements which don't have a certain parent
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
add a comment |
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
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
add a comment |
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
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
javascript jquery html jquery-selectors
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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>
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
add a comment |
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');
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
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
add a comment |
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>
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
add a comment |
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>
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>
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
add a comment |
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
add a comment |
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');
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
add a comment |
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');
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
add a comment |
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');
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>
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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