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

113
Vistas
Change array value inside of function in an element in javascript

I am trying to change an array value thats inside of a function of my button. I want the first number in the array to change but can't seem to get it working

So i want my HTML to go from this:

<button onclick="skillUpgrade([50,5])">Upgrade your skill</button>

To this:

<button onclick="skillUpgrade([55,5])">Upgrade your skill</button>

Here is my javascript

function skillUpgrade(el) {
  roundDown = Math.floor(money / el[0]);
  if (memes >= el[0] * checkTimes) {
    baseClicks = baseClicks + (el[1] * checkTimes);
    money = money - (el[0] * checkTimes);
    newPrice = el[0] * 1.1;
    el[0] = newPrice;
    console.log(el[0])
    document.getElementById("moneyAmount").innerHTML = money;
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It is generally not a good idea to use inline event handlers. Here is a minimal reproducable example. It uses a data-attribute to store values within the button and event delegation to handle the button click.

document.addEventListener(`click`, handle);

function handle(evt) {
  // do something if the click originated from button#upgradeSkill
  if (evt.target.id === "upgradeSkill") {
    const btn = evt.target;
    const currentValue = btn.dataset.values.split(`,`)
      .map(Number);
    //     ^ convert values to Number                                        
    currentValue[0] += 5;
    // reset the values
    // note: the array is automatically converted back to string
    btn.dataset.values = currentValue;
  }
}
#upgradeSkill:after {
  content: ' (current 'attr(data-values)')'; 
}
<button id="upgradeSkill" data-values="50,5">Upgrade your skills</button>

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