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

136
Visualizações
Find a DOM Element using an attribute-value which is acquired dynamically?

There is already question about getting a dom element by using an attribute value. But mine is different because my attribute value is not known beforehand.

<div class='gameBoard'>
   <div class="grid" index='0'></div>
   <div class="grid" index='1'></div>
   <div class="grid" index='2'></div>
   <div class="grid" index='3'></div>
   <!-- Goes up to 100 div / 99 index values -->
</div>

I have a div gameboard representing a dom element which contains 100 div, each with the index values from 0 to 99. Below im looping through all the divs and when the mouse hovers over a div, than I gets its index ( data-attribute ). ( if the direction is on the x-axis, then I do this )

            const one = el;
            const onei = el.getAttribute('index');
            const two = el.nextElementSibling;
            const twoi = two.getAttribute('index');

But when the direction is on the y-axis, I don't need the next div element but the one below. For ex: if index is 0, I need 0 + 10 = 10, so i need the div with index 10. I tried the code below but it is not working cuz the value is retrieved dynamically, I tried using string interpolation and it doesn't work. So please help !!!

    const grids = document.querySelectorAll('.grid');

    el.addEventListener('mouseover', (e) => {
    if (foo === 'destroyer' || foo === 'submarine') {
       if (currentDirection === 'Y') {
            const one = el;
            const onei = Number(el.getAttribute('index'));
            const twoi = onei + 10;
            const two = document.querySelector("[index='twoi']");
            console.log(two, twoi);
        }
    }
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The string concatenation inside querySelector is wrong

It should be working with this below code for that line

const two = document.querySelector(`[index="${twoi}"]`);
about 4 years ago · Juan Pablo Isaza Relatório

0

Your issue is that of string interpolation. You're literally looking for an element that has the index twoi. Insteady, try interpolating the string with back ticks, e.g.

const two = document.querySelector(`[index='${twoi}]'`);

or just adding the twoi string to it:

const two = document.querySelector("[index=" + twoi + "]");

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