• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Can I use different colors for "State" in WebServer 2.2 "Multiple Output Switch"?

Q&A Forum › Can I use different colors for "State" in WebServer 2.2 "Multiple Output Switch"?
1 Vote Up Vote Down
Sheldon Furst asked 5 years ago
4 Answers
0 Vote Up Vote Down
Sheldon Furst answered 5 years ago

In your WebServer module 2.2 in addition to the switch there is a “State” display that shows either ON or OFF. This is inserted into the HTML code by the Javascript snippet below:
if (state == “1”) {
document.getElementById(output).checked = true;
document.getElementById(output+”s”).innerHTML = “ON”;
}
else {
document.getElementById(output).checked = false;
document.getElementById(output+”s”).innerHTML = “OFF”;
}
Is there a way to insert a color tag so that “OFF” displays as (say) black and “ON” displays as (say) red? I tried adding style tags in front of the “ON” but that didn’t seem to work.
Thank you!

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
You can change the color of an HTML element using JavaScript.
For example, look at the lines that I’ve inserted (in bold)

if (state == “1”) {
document.getElementById(output).checked = true;
document.getElementById(output+”s”).innerHTML = “ON”;
document.getElementById(output+"s").style.color = "red";
}
else {
document.getElementById(output).checked = false;
document.getElementById(output+”s”).innerHTML = “OFF”;
document.getElementById(output+"s").style.color = "black";
}

You also need to add something similar to the toggleCheckbox() function, like this:

function toggleCheckbox (element) {
var xhr = new XMLHttpRequest();
if (element.checked) {
xhr.open("GET", "/update?output="+element.id+"&state=1", true);
document.getElementById(element.id+"s").innerHTML = "ON";
document.getElementById(element.id+"s").style.color = "red";
}
else {
xhr.open("GET", "/update?output="+element.id+"&state=0", true);
document.getElementById(element.id+"s").innerHTML = "OFF";
document.getElementById(element.id+"s").style.color = "black";
}
xhr.send();
}

 
You can learn more about that in more detail here: https://www.w3schools.com/jsref/prop_html_style.asp
Let me know if this works for you.

 
Also, can you tell me how are you feeling about this new eBook? Is it easy to follow? Is there any topic that you would like to be covered in a future version?
 
Regards,
Sara

 

0 Vote Up Vote Down
Sheldon Furst answered 5 years ago

Awesome, Sara, that was easy! I’m certainly a novice at Javascript.
I love the new eBook on servers! I’ve been playing with the code you and Rui have posted on RNT for months but this book has really helped me understand what I was doing. And, despite the disclaimer that the book is not a tutorial on HTML, etc, it has wonderful, clear explanations that have really advanced my understanding of HTML and CSS.
Thank you for all the great tutorials you two have done. I’m a huge ESP8266/ESP32 fan and your website is my go-to reference. Keep up the great work!

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
That’s great!
 
I’m really glad that you are enjoying the new eBook. We put a lot of effort to make things as clear as possible for beginners.
Thank you for the trust in our tutorials.
 
I’ll close this issue. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.