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

279
Vistas
¿Cómo almacenar y establecer los valores en JS?

Tengo un HTML como el de abajo,

 <input class="btn btn-primary" id ="b1" type="button" value="Blue"> <input class="btn btn-primary" id ="b2" type="button" value="orange"> <input class="btn btn-primary" id ="b3" type="button" value="green">

No quiero tomar el valor del HTML. Como el de abajo,

 $(#b1).attr('value') or var val = document.getElementById(ele.id).value;

En cambio, I need the value from the JS .

El default Value para cada botón debe ser 0 . Cuando hago clic en el botón cada vez, ++default value . Cuando llegue a 3 , nuevamente debería moverse a 0 .

Me gusta,

 default value = default Value >= 3 ? 0 : ++defaultValue
 $("button").click(function () { buttonVal(this); }); function buttonVal(ele) { var def = []; def = def>=3 ? 0 : ++def; switch(def) { case 0: def = 5; break; case 1 : def = 4; break; default: def = 0; } }

Pero usé switch case para obtener el default value .

Esta línea def = def>=3 ? 0 : ++def; se ejecuta antes del cambio.

Entonces, no funcionó .

¿Podemos implementar de diferentes maneras además de switch para obtener el valor predeterminado?

Necesito almacenar el valor predeterminado en el JS .

¿Alguien podría ayudar?

Muchas gracias.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Utilice el conjunto de dataset del botón para almacenar el valor.

 $("button").click(function () { let currentValue = this.dataset.value || 0; if (++currentValue > 3) { currentValue = 0; } this.dataset.value = currentValue; ... });
about 4 years ago · Juan Pablo Isaza Denunciar

0

Teniendo en cuenta la identificación de su botón como clave para almacenar el número de conteo

 const container = document.getElementById("btn-container"); const btnValue = {}; container.addEventListener("click", function(e){ // const v = e.target.value; if(e.target.tagName.toLowerCase() === "input"){ const v = e.target.id; btnValue[v] = !btnValue[v] ? 1 : btnValue[v]==3 ? 0 : btnValue[v]+1; // Uncommment below code to show value on button // e.target.value=btnValue[v]; } console.log(btnValue); });
 <div id="btn-container"> <input class="btn btn-primary" id ="b1" type="button" value="Blue"> <input class="btn btn-primary" id ="b2" type="button" value="orange"> <input class="btn btn-primary" id ="b3" type="button" value="green"> </div>

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