so, I'm literally new to JavaScript, I've been reading and searching about it in here and other places.
This may be super easy, but I'm stuck and I don't know how to get it done.
Here is the deal. I'm trying to get the parent's name through the child id/class/tag. I'll demonstrate.
<li class="example">example</li>
<ul id="what i want">what i want</ul>
<li id="something here">something here</li>
<img class="element i'm using">element im using</img>
So, I can get, through document.getElementById
, the "element i'm using". I want to get the "what i want". I can't really use the same thing because that element has some random numbers that I can't predict.
My plan is to use "element i'm using" to get "what i want" and apply a rule.
I'm a literally beginner, if it's someting basic I'm sorry, but I couldn't find a solution yet.
I hope you guys can help me.
Thanks, have a good day.
--- Edit/Update ---
Hey, I quick update.
I've found a way to do what I want. I'll explain better, but I'm struggling again.
Here we go:
<li class="bp-body">
<ul id="random-number-1">
<li class="voice">
<img class="100-photo">(photo)</img>
</li>
</ul>
<div id="white-line"></div>
<ul class="random-number-2">
<li id="voice">
<img class="101-photo">(photo)</img>
</li>
</ul>
<div id="white-line"></div>
<ul id="random-number-3">
<li class="voice">
<img class="102-photo">(photo)</img>
</li>
</ul>
<div id="white-line"></div>
<ul id="random-number-4">
<li class="voice">
<img class="100-photo">(photo)</img>
</li>
</ul>
</li>
So, my original problem was that I was trying to get the <ul id="random-number-1">
through the <img class="100-photo">
. And I've done that. I used a function to return the document.querySelector("100-photo")
parent, which is <li class="voice">
, and to get the node I wanted I ran the function with .parent, and it returned the <ul id="random-number-1">
so I could set a property. It worked fine, but I've found another problem: this solution only returns the first <ul>
that contains the <img class="100-photo">
on node tree, but in the reality the <li class="bp-body">
may have others <ul id="random-number-xxxx">
.
Here is the function that I used:
function findParent() {
return document.querySelector("100-photo").parentNode;
}
findParent().parentNode.style.setProperty('background', '#fff');
The element <ul id="random-number-1">
got a nice white background, but the other elements with the same <img class="100-photo">
stayed the same. I tried to find a workaround.
I tried the querySelectorAll first to find all the elements, I used console.log to see if it works.
function getParents() {
return document.querySelectorAll("100-photo");
}
It returned in the console:
NodeList [ img.100-photo , img.100-photo ]
Great, that's a start. But when I tried to do the same trick, it didn't work.
function getParents() {
return document.querySelectorAll("100-photo").parentNode;
}
The console only shows: undefined
.
I thought that with a loop it may work with the solution, but I'm not with it familiar yet. I tried to workaround something to at least get a NodeList with <li class="voice">
which contains the <img class="100-photo">
but I got nothing yet.
My objective is to identify every <img class="100-photo">
to get the respective <li>
parent to get the <ul id="random-number-xxxx">
to set a property.
I hope you guys can help me.
Thanks, have a nice day.
` as a sibling (and neither should have `
` as a sibling)