I have started with javascript for frontend development and I'm trying to alter an icon in a Moodle course with custom js code but I can't target it. I tried to navigate there from a known start point (I created a span with an id) and then wanted to go up 4 steps and grab the image with find(). But I can't target any child from the grand-grand-grand-parent at all it seems.
I loaded jquery before using the code below and of course there is more code for the whole Moodle site.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li class="activity hvp modtype_hvp " id="module-1083815">
<div id="yui_3_17_2_1_1637060405097_363">
<div class="mod-indent-outer w-100" id="yui_3_17_2_1_1637060405097_362">
<div class="mod-indent">
</div>
<div id="yui_3_17_2_1_1637060405097_361">
<div class="activityinstance">
<a class="aalink dimmed" onclick="" href="https://moodle.htw-berlin.de/mod/hvp/view.php?id=1083815">
<!-- I want to change this image -->
<img src="https://moodle.htw-berlin.de/theme/image.php/boost_campus/hvp/1636530372/icon" class="iconlarge activityicon" alt="" role="presentation" aria-hidden="true">
<span class="instancename">Business Proficiency - Telephoning
<span class="accesshide "> Interactive Content</span>
</span>
</a>
</div>
<div class="contentafterlink dimmed_text" id="yui_3_17_2_1_1637060405097_360">
<div class="no-overflow" id="yui_3_17_2_1_1637060405097_359">
<div class="no-overflow" id="yui_3_17_2_1_1637060405097_358">
<script type="text/javascript">
document.write("<span id='targetchild101'>findmyparent</span><br>");
document.write($("#targetchild101").parents()[3].tagName);
<!-- I can't target children() or find() children from the parent -->
$("#targetchild101").parents()[3].find("img").src = "https://moodle.htw-berlin.de/theme/image.php/boost_campus/forum/1636530372/icon";
</script>
</div>
</div>
</div>
</div>
</div>
</div>
</li>
My main question is: how can I target the image that is a grand-child from a grand parent to change its source. I get the error "Uncaught TypeError: $(...).parents(...)[3].find is not a function" So what do I have to do to find a child of a parent instead of using parents()[3]?
My side question is: Is there a more elaborate way to get the reference point than to create an empty span with id? Something like a $(document).current function?