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

157
Views
Toggle Button text After Selecting Option HTML

So, I've been trying to toggle a button text after selection an certain option.

However no matter what I try the button text doesn't change.

I have this HTML code:

<select id="temp">
        <option value="selectOption" disabled selected>Select</option>
        <option value="farenheit">Farenheit</option>
        <option value="kelvin">Kelvin</option>
</select><br>

<button id="buttonTemp" onclick="converterTemp()">Convert</button><br>

And this is the JS code:

const slt = document.querySelector('select');

switch (slt.value) {
    case 'farenheit':
        document.getElementById('buttonTemp').innerHTML = 'Convert to Fº';
        break;

    case 'kelvin':
        document.getElementById('buttonTemp').innerHTML = 'Convert to kº';
        break;
}

It was supposed to toggle the text "Convert" to "Convert to Fº" / "Convert to Kº" on the user selection of Farenheit or Kelvin.

Could someone help me, please?

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

0

Put the switch code into an event listener, that listens for a change in the dropdown.

const slt = document.querySelector('select');

slt.addEventListener('change', () =>
{
  switch (slt.value) {
      case 'farenheit':
          document.getElementById('buttonTemp').innerHTML = 'Convert to Fº';
          break;

      case 'kelvin':
          document.getElementById('buttonTemp').innerHTML = 'Convert to kº';
          break;
  }
})
<select id="temp">
        <option value="selectOption" disabled selected>Select</option>
        <option value="farenheit">Farenheit</option>
        <option value="kelvin">Kelvin</option>
</select><br>

<button id="buttonTemp" onclick="converterTemp()">Convert</button><br>


  

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!