2
I have a list of items (that comes from the database), I need to compare each item of one of these and if the value is 0 change the text. Below is the function I’m trying to use to change the value of the item.
function isFree(){
var preco = document.getElementsByClassName('comprar');
for(n = 0; n < preco.length; n++){
var validaPreco = preco[n].innerHTML;
if(validaPreco == '$ 0'){
validaPreco = 'Grátis'
}
}
}
When I use the line document.getElementsByClassName('comprar')[0].innerHTML
in the browser console I have returned the value I want to use to compare, but I want to compare all the items that are brought from the database.
And this is the code line where the price appears
<a href="detalhe/{{item.id}}" onclick="subir()"><button class="comprar">$ {{item.originalValue}}</button></a>
Just to be clear, I am using Polymer 2, the firebase-query element to bring the data.
You can’t call the event
change
of the text boxes updating these.comprar
? In the case, where appropriate, the quantity?<input type="text" id="qtd1" ... onchange="isFree()" />
– Marcelo Shiniti Uchimura
I think there’s a misconception in your code, on the line where it is
validaPreco = 'Grátis';
should bepreco[n].innerHTML = 'Grátis';
– Marcelo Shiniti Uchimura
Objects are passed by reference, in JS, while properties with primitive types are passed by value. When you did
var validaPreco = preco[n].innerHTML
, you just managed to usepreco[n]
as if it were the very object he keeps because it is a passing representation by reference.preco[n].innerHTML
, in turn, it hasString
and is passed by value.– Marcelo Shiniti Uchimura
That’s right, you really should exchange the validationPreco for the price[n]. Thanks for the help and the explanation.
– Matheus Ribeiro