Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

Finally I have found the solution:

const amountClass = '.' + coinName;
    const existingCoin = document.querySelectorAll(amountClass.replace(/ /g,''));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda