Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
Cómo funcionará la condición si el valor del atributo y el valor de ID son los mismos a través del ciclo
<style> .heading { color: red; } .button { font-size: 18px; } .skyblue { background-color: skyblue; } </style>
Si el "data-mh" de la etiqueta <h1> y la identificación de la etiqueta <button> tienen un atributo similar que si hago clic en el botón, el color de fondo del botón cambiará. Y el atributo se imprimirá en la etiqueta P. Mi código funciona con el primer botón pero no funciona con el segundo u otros botones. /* como puedo hacerlo por bucle */
 <h1 class="heading" data-mh="item1">Hello World</h1> <button class="button" id="item1">Click Here</button> <h1 class="heading" data-mh="item2">Hello World</h1> <button class="button" id="item2">Click Here</button> <h1 class="heading" data-mh="item3">Hello World</h1> <button class="button" id="item3">Click Here</button> <p id="demo"></p> <!-- This Paragraph tag will print my attribute --> <!-- HTML End --> <script> var x = document.querySelector(".heading").getAttribute("data-mh"); // This variable is to get Attribute of "data-mh" var y = document.querySelector(".button"); // Button selection. By clicking this button I will get print my Attribute var z = y.getAttribute("id"); // Getting button id to compaire with "data-mh" attribute y.onclick = function() { //If X (ID) and Z (Attribute) matching then working this Condition if (x == z) { y.classList.toggle("skyblue"); document.getElementById("demo").innerHTML = x+" "+z; // This line of code will print my Attribute on P tag } } </script>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Este código hace la tarea recorriendo cada botón y haciendo toda la configuración para los detectores de eventos y la comparación.

 // select all the buttons const buttons = document.querySelectorAll(".button"); // loop over each one for(const button of buttons) { // add an on click listener button.addEventListener("click", (event) => { // get the button ID const x = button.getAttribute("id"); /// get the previous element's (the heading's) data-mh attribute const z = button.previousElementSibling.getAttribute("data-mh"); // compare them if(x == z) { button.classList.toggle("skyblue"); document.getElementById("demo").innerHTML = x+" "+z; } }); }

Aquí hay una demostración: https://jsfiddle.net/w3d0z8Ly/

about 4 years ago · Juan Pablo Isaza Denunciar

0

revisa mi respuesta:-

 var x = document.querySelectorAll(".heading"); // This variable is to get Attribute of "data-mh" var y = document.querySelectorAll(".button"); // Button selection. By clicking this button I will get print my Attribute // var z = y.getAttribute("id"); // Getting button id to compaire with "data-mh" attribute function btnClick(e) { for(var i = 0; i < x.length; i++) { if(e.getAttribute("id") == x[i].getAttribute("data-mh")) { document.getElementById("demo").innerHTML = x[i].getAttribute("data-mh"); } } for(var j = 0; j < y.length; j++) { y[j].classList.remove("skyblue"); } e.classList.add("skyblue"); }
 .heading { color: red; } .button { font-size: 18px; } .skyblue { background-color: skyblue; }
 <h1 class="heading" data-mh="item1">Hello World</h1><button class="button" id="item1" onClick="btnClick(this)">Click Here</button> <h1 class="heading" data-mh="item2">Hello World</h1> <button class="button" id="item2" onClick="btnClick(this)">Click Here</button> <h1 class="heading" data-mh="item3">Hello World</h1> <button class="button" id="item3" onClick="btnClick(this)">Click Here</button> <p id="demo"></p>

Por favor, chequee esto

about 4 years ago · Juan Pablo Isaza Denunciar

0

const buttons = document.querySelectorAll(".button"); const headings = document.querySelectorAll(".heading"); for (const button of buttons) { for (const heading of headings) { button.addEventListener("click", (event) => { const x = button.getAttribute("id"); const z = heading.getAttribute("data-mh"); if (x == z) { button.classList.toggle("skyblue"); document.getElementById("demo").innerHTML = x + " " + z; } }); } }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda