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!