Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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 answers
Answer question

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 Report

0

Finally I have found the solution:

const amountClass = '.' + coinName;
    const existingCoin = document.querySelectorAll(amountClass.replace(/ /g,''));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!