Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
querySelectorAll with variable doesn't select the element

I want to select all elements using querySelectorAll with variable "amountClass" but NodeList always is empty

    function addingCoin() {
const table = document.querySelector('.list__table');

table.addEventListener('submit', (event) => {
    event.preventDefault();
    
    
    const walletCryptos = document.querySelector('#usersCrypto');
    const coinAmount = event.target.inputAmount;
    const coinRow = coinAmount.parentElement.parentElement.parentElement;
    const coinName = coinRow.querySelector('.name').textContent;
    const coinPrice = coinRow.querySelector('.price').textContent.match(/\d+(\.\d+)?/)[0];
    const coinValue = coinAmount.value*coinRow.querySelector('.price').textContent.match(/\d+(\.\d+)?/)[0];
    
    let amountClass = coinName;
    let existingCoin = document.querySelectorAll(amountClass);

    if (existingCoin.length > 0) {
        existingCoin[0].innerText = coinAmount.value + 
    existingCoin[0].value;

    } else {
    const newTr = document.createElement("tr");
    const nameTh = document.createElement("th");
    const priceTh = document.createElement("th");
    const amountTh = document.createElement("th");
    const valueTh = document.createElement("th");

    nameTh.innerText = coinName;

    if (coinPrice < 0.95) {
        priceTh.innerText = parseFloat(coinPrice).toFixed(8);
    } else {
        priceTh.innerText = parseFloat(coinPrice).toFixed(2);  
    };

    amountTh.innerText = coinAmount.value;
    
    amountTh.className += coinName;

    if (coinValue < 0.95) {
        valueTh.innerText = parseFloat(coinValue).toFixed(8);
    } else {
        valueTh.innerText = parseFloat(coinValue).toFixed(2);  
    };

    walletCryptos.appendChild(newTr);
    walletCryptos.appendChild(nameTh);
    walletCryptos.appendChild(priceTh);
    walletCryptos.appendChild(amountTh);
    walletCryptos.appendChild(valueTh);
    }});
    };

I think the problem may be with this part of code:

    let existingCoin = document.querySelectorAll(amountClass);

What can i change to make this work properly? Is there any other solution to check does the element with the certain class exist?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need a . at the beginning to select a class.

Trim the text content in case it has whitespace around the text.

const coinName = coinRow.querySelector('.name').textContent.trim();
let amountClass = '.' + coinName
about 4 years ago · Juan Pablo Isaza Relatório

0

Finally I have found the solution:

const amountClass = '.' + coinName;
    const existingCoin = document.querySelectorAll(amountClass.replace(/ /g,''));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda