Help me fix the script

Started by miaedwards, Sep 03, 2022, 01:43 AM

Previous topic - Next topic

miaedwardsTopic starter

There is a simple script that loads the desired text (date) from the desired element

<span id="data">date loading...</span>
the script itself:
elm = document.getElementById('data');
elm.innerHTML = "May 19, 21-00"

And how to do it correctly if there are many such elements on the page?
 for instance, to replace all span with the class class="data"

I found that there is such document.getElementsByClassName - but it doesn't work for some reason, and they write that it doesn't work in all browsers. It is necessary that everyone and everyone in mobile, including.

noob in scripts


Judging by the code, you don't have a class there, but an id with data. And there should be only one id.
generally, you need to cycle through all the data on the page. That's how it will work.

Here I threw a simple script:
The required elements must have the "data" class.
<span class="data">date loading...</span>
<span class="data">date loading...</span>
<span class="data">date loading...</span>

Below these elements in the page code, the script itself is located, which will go through all of them in a loop:
<script type="text/javascript">
var elem = document.querySelectorAll('.data');
for (i = 0; i < elem.length; i++) { elm[i].innerHTML='May 19, 21-00'; }