Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda