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

89
Visualizações
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
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