I’ve write a simple JavaScript code to show time. I want to show this javascript value or output in multiple place. That’s why I followed querySelectorAll method. But it now working…
Look at my code;
`<p class="mm"></p>
<h1 class="mm"></h1>
<script type="text/javaScript">
var date = new Date();
document.querySelectorall(".mm").innerHTML = date;
</script>`
How can I do it without getElementById……? If I add querySelector, it just show one place. Not many place.
querySelectorAll not working. I want to show date in multiple place.
>Solution :
Here’s an example using a for...of
loop to help you reproduce success. You can search MDN to learn about other JavaScript and web APIs.
const date = new Date();
for (const element of document.querySelectorAll(".mm")) {
element.textContent = date.toLocaleString();
}
<p class="mm"></p>
<h1 class="mm"></h1>