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

185
Views
¿Cómo capturar el cambio de 'valor de datos' en la etiqueta <button>? (html, js)

¿Cómo capturar el cambio de 'valor de datos' en la etiqueta? (html, js)

// caja desplegable

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

Necesito 1), 2), 3)

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

pero el evento de cambio admite SOLO la entrada, la etiqueta de texto no el valor de datos y el evento de clic no funciona. no cambia el valor de los datos justo después del evento 'clic'.

Entonces, ¿CÓMO obtengo el evento de cambio de 'valor de datos'?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Para detectar cambios en los atributos de los elementos, puede usar un 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 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!