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

216
Vistas
Onclick button running automatically after $() insertion

I'm 99% through done writing this code but I'm stuck on the last hurdle.

The main issue here is: I have 2 HTML buttons which aim to change text based on the function output, successful or not. Currently, the logic for the functions is working - ie the expected returned values are as expected. However, when I try to move these functions into my HTML file, they automatically run, which is overwriting the default value and defeating the point of having the button.

Button:

<button id="goldButton" onclick = "${calcGoldTierStatus(exampleCustomer)}">Click me!</button> 
    <h2 id="goldResult">Gold tier eligibility:${goldTierEligible}</h2>

Function earlier in same script.js:

function calcSilverTierStatus() {
  if (
    exampleCustomer.previousStays >= silverTier.previousStays &&
    exampleCustomer.bookingAgencyId === null || String || Number &&
    (exampleCustomer.roomSpend / (exampleCustomer.roomSpend.length+=1)) >= 100 &&
    exampleCustomer.reservationNights >= silverTier.previousStays
  ) {
    silverTierEligible = ("Silver Eligibility:Eligible for Silver tier!")
  } else {
    silverTierEligible= ("Silver Eligibility:Not eligible for Silver Tier")
  }
}

What may be noteworthy is that I am passing the values into HTML via declaring a const 'content', then using:

document.body.innerHTML = content;

to write the HTML body. It seems to be the $(calcGoldTierStatus(exampleCustomer)} that is causing the autorun, but I had no idea how to otherwise move the function into HTML from JS.

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

0

I found from some posts that it is bad practice to use template literals inside onclick buttons. Instead, using a combination of event listeners, getElementById, and appending a HTML article object instead of replacing main, solved most my problems.

@me for a look at the exact ways I got around this.

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