Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript querySelectorAll not working with class

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading