1
Something strange is happening, I’m using foreach to add values directly by the ID, only the strange and the function only works if the array is inside a bracket.
var elements1 = document.getElementsByClassName("rating1");
var array1 = ["9.0", "9.5", "7.1"];
for (var i = 0; i < elements1.length; i++) {
elements1[i].innerHTML = array1[i];
}
var elements2 = document.getElementsByClassName("rating2");
var array2 = ("9.0", "9.5", "7.1");
for (var i = 0; i < elements2.length; i++) {
elements2[i].innerHTML = array2[i];
}
<h2>Aqui funciona perfeitamente</h2>
<div class="rating1"></div>
<div class="rating1"></div>
<div class="rating1"></div>
<br>
<h2>Aqui não funciona</h2>
<div class="rating2"></div>
<div class="rating2"></div>
<div class="rating2"></div>
This is just an example of what’s going on.
The problem and the next, I’m using the imdb plugin to capture the qualification of films.
When I make the request the system returns me the data, I believe it is in JSON, all the qualifications that were requested comes together within a value called "rating".
I am using the same code as above, the only thing that changes and I am using the array that comes in the value "rating":
var array1 = rating;
Only instead of working as in the first example, for some reason it is the second example.
That casts a
TypeError: array2 is undefined
for me, are you sure you’re right? It seems very strange too...– GBrandt
Right, fixed now missing instantiating the array
– ScrapBench
really works the way you said, but only using the data manualment and in the console.log, using the data that comes from the request in JSON does not work, only appears Undefined.
– Vitor Hugo
this data that comes from the request may be Object, so it is not working, but I am layman as to js.
– Vitor Hugo
need to parse with JSON.parse(objeto_json)
– ScrapBench