0
I have a function that is responsible for updating the values in some <div>
, follows script:
file js.
window.onload = function makeRequest() {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
calcPreco(xmlHttp.responseText);
}
xmlHttp.open("GET", "_config/buscar_valor.php?id="+document.getElementsByName("cloud")[0].getAttribute("cloudid")+"&periodicidade=monthly", true); // true para asynchronous
xmlHttp.send(null);
}
function calcPreco(preco) {
console.log(preco);
preco = preco.replace(",", ".");
preco -= document.getElementsByName("cloud")[0].getAttribute("desconto");
document.getElementsByClassName("mostrar_valor").textContent = preco;
}
index php.
<div name="cloud" cloudid="1" desconto="5">
<span class="mostrar_valor"></span>
</div>
<div name="cloud" cloudid="2" desconto="10">
<span class="mostrar_valor"></span>
</div>
<div name="cloud" cloudid="3" desconto="15">
<span class="mostrar_valor"></span>
</div>
Note that only attributes cloudid
anddesconto
are changed in each <div>
, the rest remains the same.
The script will only do a calculation by looking for the value in "** lookup.php value **", using the attribute cloudid
, which is the ID of each plan.
The attribute desconto
is the value it subtracts from the account.
The problem is he’s doing it just for the first <div>
, how can I make it work for all <div>
?
UPDATE:
window.onload = function calcPreco() {
for(const cloud of Array.from(document.getElementsByName("cloud"))) {
fetch("_config/buscar_valor.php?id="+cloud.getAttribute("cloudid")+"&periodicidade=monthly")
.then(res => res.text())
.then(preco => {
preco -= cloud.getAttribute("desconto");
const valor = cloud.querySelector(".mostrar_valor");
})
}
}
@Iazyfox... Thanks for the reply, it was a friend of mine who put together this script, I don’t have much knowledge in the area yet. You can show me how this implementation would look in full code?
– Wendler
Which part of the code is struggling?
– lazyFox
I don’t know how to implement these loops in the function, I don’t know how the script should look.
– Wendler
My answer clarifies your doubt. I don’t want to be rude but I think you should study a little more javascript and familiarize yourself with some concepts and mechanics of it. If I code here you will not learn anything. Anyway, here are some lights: Selecting elements from a page - Ciclo for
– lazyFox
@Iazyfox... really researching I managed to do something. I managed to make it work another way, similar to the use, based on some searches, I just couldn’t get it to print the result in <span>, can you see what I did wrong? I updated the question.
– Wendler
I don’t have a computer with me, but right now all you have to do is enter the value in the
span
try itinnerHTML
orinnerText
, That is to saycloud.querySelector(".mostrar_valor").innerHTML = valorDesejado
– lazyFox
Perfect, has how to convert this value to comma, since it will print with dot?
– Wendler