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

174
Visualizações
get data-value with variable value

I have a data-value in my php code that is used in several places, for example:

<a class="dropdown-item" data-value="<?= $i ?>"></a>
<a class="dropdown-item" data-value="<?= $k ?>"></a>

And now I need to use one of these values ​​in the script

getAttribute('data-value')

But I can’t just use data-value, since it will be relevant to everything that I have, I only need to use this data-value="<?= $k ?>"

How can I specify a value with this variable in the script?

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

0

since you want to get the data-value of a specific element with the $k variable, your best bet is giving an id to that specific element and selecting it by id:

HTML

<a id="element_with_k" class="dropdown-item" data-value="<?= $k ?>"></a>

JS

const elementWithK = document.getElementById('element_with_k');

elementWithK.dataset.value // "the content of $k"

In case thete are multiple elements that you want to select then you add an extra class to those elements, query all the elements that have the extra class , loop through them and get each element's value.

Hope this helps, cheers!

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