Answer the question
In order to leave comments, you need to log in
Is it possible to place JS above HTML and then wait and apply to the DOM element that appears?
Greetings
Is there any function in JS that allows you to wait for a certain element on the page to load, and when it appears, execute a function? Let me explain. The structure of the document is something like this:
<head>
<script>
var myElement = document.querySelector('.two');
myElement.setAttribute("style", "color: red;");
</script>
</head>
<body>
<p class="one">one</p>
<p class="two">two</p>
<p class="three">three</p>
</body>
Answer the question
In order to leave comments, you need to log in
You can wait for the entire DOM, I would even say I recommend
document.addEventListener("DOMContentLoaded", function(){
var myElement = document.querySelector('.two');
myElement.setAttribute("style", "color: red;");
});
there are several options here:
https://stackoverflow.com/questions/17071697/how-t...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question