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

85
Views
Toggle button both by click and by function

I got a problem switching a button both ways, by clicking on it and by changing it by a js-function which reacts on recieved mqtt messages and toggles the button. The first part works, the latter not. Here is my html

<label class="switch">
<input type="checkbox" value="On" id="onoff" onclick="onoff_dev();">
<span class="slider round"></span>
</label>

The js to toggle the button:

function onoff_dev() {
    currentvalue = document.getElementById('onoff').value;
    if (currentvalue == "Off") {
        document.getElementById("onoff").value = "On";
        switch("0");
    } else {
        document.getElementById("onoff").value = "Off";
        switch("1");
    }
}

And here the function, which is executed when a mqtt message arrives. The message is either 'r_message = On' or 'r_message = Off' It should change the slide button, but "document.getElementById('onoff').value = "On";" does not update the value of the button.

function onMessageArrived(r_message) {
    if (r_message == "On") {
        document.getElementById('onoff').value  = "On";
    }
    if (r_message == "Off") {
        document.getElementById('onoff').value  = "Off";
    }
}

Can you please help me? Thanks!

about 4 years ago · Juan Pablo Isaza
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!