Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Basic DOM/JS syntax issue - element.closest is not a function

JRR

New Coder
Hi, I'm really basic in my JS knowledge, but I usually get by with some research here and there, but I just can't figure this out, and I know it's a really simple issue, I just can't find a solution online anywhere. I've tried a load of different permutations, calling functions, etc. I'm just getting more and more in the weeds. Here's the issue:

I need to add a background to an element that I don't have direct control over. So, I am adding a child DIV into that element and using the following code snippet in a SCRIPT tag in the head of my template to add a class to the grand-parent element:

JavaScript:
<script type="text/javascript">
const greyback = document.getElementsByClassName('shaddowChild');


greyback.closest('td').classList.add('backShaddow');


</script>

For some reason, the browser console just keeps throwing "Uncaught TypeError: greyback.closest is not a function" as if it doesn't recognise that closest() isn't a function. I know it's all just down to how I structure this basic script, but I just don't know enough about what I'm doing to get it to work.

Thanks in advance for your help!!!
 
Solution
D
Looping trough your array of elements is the easiest of things:

JavaScript:
for (const greyback of document.getElementsByClassName('shaddowChild') )
  greyback.closest('td').classList.add('backShaddow');
}
Try making shaddowChild an id and then instead getElementById.

Also why can't you just do it like this:
JavaScript:
document.getElementsByClassName("shaddowChild").style.background = "#FFFFFF"; //put any colour or image you want here
 
I think the reason you get the error is because closest() is a method for a single DOM element. Your var greyBack is not an element, but an array of elements (even if there is only one element with classname "shaddowChild"). So I think it stands to reason you cannot invoke the closest() method on it. If that is indeed the case, the error message is pretty lame though...
 
I think the reason you get the error is because closest() is a method for a single DOM element. Your var greyBack is not an element, but an array of elements (even if there is only one element with classname "shaddowChild"). So I think it stands to reason you cannot invoke the closest() method on it. If that is indeed the case, the error message is pretty lame though...
That makes the most sense. So, I need to find a way to run through the array of elements to change them all. If you have any suggestions, I'd be grateful, but at least I have a clue now, thanks!
 
Try making shaddowChild an id and then instead getElementById.

Also why can't you just do it like this:
JavaScript:
document.getElementsByClassName("shaddowChild").style.background = "#FFFFFF"; //put any colour or image you want here
It really doesn't matter if I use getElementById or getElementByClassName, that isn't the sticking point. I'm using class as the element identifier because I don't want to have to add a different id to each section I want given the same style (though I may have to if I can't find an appropriate way to cycle through the array of class elements on the page).

As for the inline styling, that would not achieve the effect I need as it ignores the parent entirely. That is the whole purpose of using closest(), to target the distant parent element that I can't directly edit in the HTML.
 
Looping trough your array of elements is the easiest of things:

JavaScript:
for (const greyback of document.getElementsByClassName('shaddowChild') )
  greyback.closest('td').classList.add('backShaddow');
}
 
Solution

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom