5
I’m in a situation where I need to take all the "parent elements" of a div, but I can’t take them all, because it would not solve my problem. I wonder if I can put a limit on getting these elements. With an example it becomes clearer. Consider the structure:
<div class="all">
<div class="1">
<div class="2">
<div class="3">
<div class="4">
<!-- Conteúdo -->
</div>
</div>
</div>
</div>
</div>
In that case I’d like to take all the parent elements of the div with the class 4
, with the exception of the class div all
but I also want all those above .all
. It is possible to do this via jquery?
@Miguel I prefer my way :)
notParent = document.querySelector(notParents[i]);
weighs a lot in the Browser– Sergio
'Cause you really are a lot more javascript pro than I am. Although I think yours has the downside of the elements '
not
' have to be classesdocument.querySelector('.' + args.shift())
– Miguel
But now that I think you wear it too
...document.querySelector(...
, will weigh more because I do not specify the type of selector?– Miguel
A version with a minor modification: https://jsfiddle.net/ks8hu650/1/
– Gabriel Katakura
@Miguel I use
document.querySelector
once to get the starting point, you had inside thewhile
, that can run many times.– Sergio
You’re right, I was in the while and I’m in the over @Sergio
– Miguel
I’ve changed, https://jsfiddle.net/ks8hu650/2/ . Lighter
– Miguel
@Miguel better, but you’re still running
.querySelector
within a loop for allnotParents
.– Sergio
But I think it’s the best way not to restrict myself only to classes/ids/tagname etc... Right @Sergio?
– Miguel
@Miguel you can ask a question about how to do
$('.4').parents().not('.all, body, html');
in native JS :)– Sergio
Done and challenging you a little more @Sergio :P
– Miguel
@Miguel aceite :P
– Sergio