I want to replace an element and the text in the element inside it with js code. But when I try this I get an error. This is because element 2 is inside element 1. But I need to use it this way and I couldn't find a solution. If you look at the sample code below, you will understand what I mean. Thank you from now.
HTML Code:
<div id="element_1">Test Div
<p id="element_2">Test Paragraph</p>
</div>
JS Code:
document.getElementById("element_1").innerHTML = "New Div Text";
document.getElementById("element_2").innerHTML = "New Paragraph Text";
Result HTML:
<div id="element_1">New Div Text
<p id="element_2">{empty}</p>
</div>
Error is:
Uncaught TypeError: Cannot set property 'innerHTML' of null
` content. Therefore it is indeed not on the DOM and null.
– Tanner Dolby Aug 04 '21 at 18:25