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

188
Visualizações
How to capture the change of 'data-value' in <button> tag ? (html, js)

How to capture the change of 'data-value' in tag ? (html, js)

// dropdown box

<button data-value="">'click'</button>
<ul>
 <li>option1</li>
 <li>option2</li>
</ul>

I need 1), 2), 3)

1) click li
2) data-value="" -> data-value="1"
3) $(button).on('change?', function() {} );

but change event support ONLY input, text tag not data-value and click event doesn't work. no change data-value right after 'click' event .

so HOW do i get the change event of 'data-value' ?

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

0

To detect changes to element attributes, you can use a MutationObserver

const btn = document.querySelector("button");

const observer = new MutationObserver((mutationList) => {
  mutationList.forEach(({ attributeName, oldValue, target, type }) => {
    if (type === "attributes" && attributeName === "data-value") {
      console.log(
        `${attributeName} changed from '${oldValue}' to '${target.dataset.value}'`
      );
    }
  });
});
observer.observe(btn, { attributes: true, attributeOldValue: true });

// Delegated event listener at <ul>
document.querySelector("ul").addEventListener("click", ({ target }) => {
  const li = target.closest("li");
  if (li) {
    const option = li.dataset.option;
    if (option !== undefined) {
      btn.dataset.value = option;
    }
  }
});
li { cursor: pointer; }
<button data-value="">'click'</button>
<ul>
 <li data-option="1">option1</li>
 <li data-option="2">option2</li>
</ul>

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