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

147
Views
Why doesn't this change el to msgTwo on a second Click of the button?

Shouldn't adding else change the content back to Welcome to our site defined in the msgTwo variable or am I doing this wrong?

let el = document.getElementById('message');
let butt = document.getElementById('clicktodo')
let msg = 'Sign up to our Newsletter for 10% off!';
let msgTwo = 'Welcome to Our Site!';

function change() {
if (el != msg) {
      el.textContent = msg;
  } else {
      el.textContent = msgTwo;
      }
};


butt.addEventListener('click', change); 
<body>
<h1>
  Traval Worthy
</h1>
<div id="message">
  Welcome to Our Site!
</div>
<button id="clicktodo" style ="padding: 10px 15px; margin-top: 15px">
  Click Here
</button>
</body>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In change function use el.textContent to get text of message element while doing check in if

if (el.textContent != msg) {
  el.textContent = msg;
} else {
   el.textContent = msgTwo;
}

You can also replace this if-else with

el.textContent = el.textContent === msg ? msgTwo : msg;
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!