Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!