Where is the green bar ?

Hi I'm trying to have a second bar, where is the mistake ?
The green bar should be below the red one.

//=====================
//HTML code for webpage
//=====================
const char webpageCont[] PROGMEM =
  R"=====(
<!DOCTYPE HTML>
<html>
<title>ESP32 WebSocket Server</title>
<!---------------------------CSS bar sise-------------------------->
<style>
    #dynRectangle
    {
        width:0px;
        height:50px;                                         // bar thickness
        top: 9px;
        background-color: red;
        z-index: -1;
        margin-top:50px;                                    // bar y position
    }
    //+++++++++++++++++++++++
  #dynRectangle2
    {
        width:0px;
        height:50px;                                         // bar thickness
        top: 9px;
        background-color: green;
        z-index: -1;
        margin-top:150px;                                    // bar y position
    }
    //++++++++++++++++++++++++
 body   {background-color:rgba(128,128,128,0.322); font-family:calibri}
    h1     {font-size: 40px; color: black; text-align: center}
    h2     {font-size: 30px; color: blue}
    h3     {font-size: 17px; color:blue}
    div.h1 {background-color: whitesmoke;}
</style>
<!--------------------------HTML subtitles-------------------------->
<body>
    <h1><div class="h1">ESP32 WebSocket Server</div></h1>
    <h2>
        POT voltage: <span style="color:rgb(216, 3, 3)" id="POTvalue">0</span> V  
    </h2>
    <h3>
        0V &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&5 &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
       &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; &emsp;&emsp; 3.5V
        <div id="dynRectangle"></div>
        <div id="dynRectangle2"></div>
    </h3>
    //++++++++++++++++++++++++++++++++++++++++++++++++
<h1><div class="h1">ESP32 WebSocket Server</div></h1>
    <h2>
        POT voltage: <span style="color:rgb(216, 3, 3)" id="POTvalue">0</span> V  
    </h2>
    <h3>
        0V &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&6 &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
       &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; &emsp;&emsp; 3.5V
        <div id="dynRectangle"></div>
        <div id="dynRectangle2"></div>
    </h3>
    //+++++++++++++++++++++++++++++++++++++++++++++++
</body>
<!----------------------JavaScript------------------------>
<script>
  InitWebSocket()
  function InitWebSocket()
  {
    websock = new WebSocket('ws://'+window.location.hostname+':81/');
    websock.onmessage=function(evt)
    {
       JSONobj = JSON.parse(evt.data);
       document.getElementById('POTvalue').innerHTML = JSONobj.POT;
       var pot = parseInt(JSONobj.POT * 250);                                           // bar length
       document.getElementById("dynRectangle").style.width = pot+"px";
        document.getElementById("dynRectangle2").style.width = pot+"px";
    }
    //+++++++++++++++++++++++++++++++++++++
 websock = new WebSocket('ws://'+window.location.hostname+':81/');
    websock.onmessage=function(evt)
    {
       JSONobj = JSON.parse(evt.data);
       document.getElementById('POTvalue').innerHTML = JSONobj.POT;
       var pot = parseInt(JSONobj.POT * 250);                                           // bar length
       document.getElementById("dynRectangle").style.width = pot+"px";
        document.getElementById("dynRectangle2").style.width = pot+"px";
    }
    //++++++++++++++++++++++++++++++++++++++++
  }
</script>
</html>
)=====";

The green bar probably is not there because of lack of data. Your script gets the exact same values two times.

Ids need to be unique. Change the second one. Dont forget to change the JS as well.

Same for dynRectangle and dynRectangle2

Maybe change from px to % to make it Independent from the resolution.

I made suggested changes but green bar still missing

//=====================
//HTML code for webpage
//=====================
const char webpageCont[] PROGMEM =
  R"=====(
<!DOCTYPE HTML>
<html>
<title>ESP32 WebSocket Server</title>
<!---------------------------CSS bar sise-------------------------->
<style>
    #dynRectangle
    {
        width:0px;
        height:50px;                                         // bar thickness
        top: 9px;
        background-color: red;
        z-index: -1;
        margin-top:50px;                                    // bar y position
    }
    
    body   {background-color:rgba(128,128,128,0.322); font-family:calibri}
    h1     {font-size: 40px; color: black; text-align: center}
    h2     {font-size: 30px; color: blue}
    h3     {font-size: 17px; color:blue}
    div.h1 {background-color: whitesmoke;}
    //++++++++++++++++++++++++++++++++++++++++
  #dynRectangle2
    {
        width:0px;
        height:50px;                                         // bar thickness
        top: 9px;
        background-color: green;
        z-index: -1;
        margin-top:250px;                                    // bar y position
    }
    
    body   {background-color:rgba(128,128,128,0.322); font-family:calibri}
    h1     {font-size: 40px; color: black; text-align: center}
    h2     {font-size: 30px; color: blue}
    h3     {font-size: 17px; color:blue}
    div.h1 {background-color: whitesmoke;}
    //++++++++++++++++++++++++++++++++++++++++++++
</style>
<!--------------------------HTML-------------------------->
<body>
    <h1><div class="h1">ESP32 WebSocket Server</div></h1>
    <h2>
        POT voltage: <span style="color:rgb(216, 3, 3)" id="POTvalue">0</span> V  
    </h2>
    <h3>
        0V &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&5 &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
       &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; &emsp;&emsp; 3.5V
        <div id="dynRectangle"></div>
    </h3>
</body>
//++++++++++++++++++++++++++++++++++++++++
<body2>
    <h1><div class="h1">ESP32 WebSocket Server</div></h1>
    <h2>
        POT2 voltage: <span style="color:rgb(216, 3, 3)" id2="POTvalue">0</span> V  
    </h2>
    <h3>
        0V &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
        &emsp;&emsp;&emsp;&emsp;&emsp;&7 &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
       &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; &emsp;&emsp; 3.5V
        <div id2="dynRectangle2"></div>
    </h3>
</body2>
//+++++++++++++++++++++++++++++++++++++++++++
<!----------------------JavaScript------------------------>
<script>
  InitWebSocket()
  function InitWebSocket()
  {
    websock = new WebSocket('ws://'+window.location.hostname+':81/');
    websock.onmessage=function(evt)
    {
       JSONobj = JSON.parse(evt.data);
       document.getElementById('POTvalue').innerHTML = JSONobj.POT;
       var pot = parseInt(JSONobj.POT * 250);                                           // bar length
       document.getElementById("dynRectangle").style.width = pot+"px";
        document.getElementById("dynRectangle2").style.width = pot+"%";
    }
  }
</script>

</html>
)=====";

Please try it like this:

id="POTvalueA"
id="POTvalueB"

not working

Is there data at the source?

yes

one bar is working copy of it not

so we will have 2 identical bars, one red one green bars

Obviously, not. Your issue is in zero data... or a bar if zero length.

What is the data source? Can you put data in the same file as the HTML?

@tom321

Hi,

There are several issues with your page as is. When trying to assemble an HTML document I suggest you put a basic skeleton for the page layout together first, and keep testing in your browser until you have something near to your design objectives.
Importantly you should also keep running the markup through an online validator to fix any errors before the markup gets to complex. I've done a skeleton example, of your page to show what I mean. Pick the 'bones' out of it and add your websocket handlers etc.

<!DOCTYPE HTML>
<html>

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>ESP32 WebSocket Server</title>

    <style>

    .dynRectangle1 {
      width: 0px;
      height: 50px; // bar thickness
      background-color: red;
      z-index: -1;
      margin-top: 50px;
      margin-bottom: 50px; // bar y position
    }

    .dynRectangle2 {
      width: 0px;
      height: 50px; // bar thickness
      background-color: green;
      z-index: -1;
      margin-top: 50px; // bar y position
    }

    body {
      background-color: rgba(128, 128, 128, 0.322);
      font-family: calibri
    }

    h1 {
      font-size: 40px;
      height:60px;
      color: black;
      text-align: center
    }

    h2 {
      font-size: 30px;
      color: blue
    }

    h3 {
      font-size: 17px;
      color: blue
    }

    </style>

    <script>
    document.addEventListener('DOMContentLoaded', () => {
      console.log("DOM LOADED");
    })

    var count = 0;

    function barGen() {

      var pot1 = Math.round(Math.random() * 500);
      var pot2 = Math.round(Math.random() * 500);
      document.getElementById('dynRectangle1').style.width = pot1 + "px";
      document.getElementById('dynRectangle2').style.width = pot2 + "px";
      if (count < 20) setTimeout(barGen, 1000);
      count++;
    }

    window.onload = function() {
      setTimeout(barGen, 1000);
    }

    </script>

  </head>

  <body>
    <div style="height:60px;background-color:skyblue;">
    <h1>ESP32 WebSocket Server</h1>
    </div>
    <h2>
      POT voltage: <span style="color:rgb(216, 3, 3)" id="POTvalue">0</span> V
    </h2>
    <h3>
      0V &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
      &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
      &emsp;&emsp;&emsp;&emsp;&emsp;&5 &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
      &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; &emsp;&emsp; 3.5V
    </h3>

    <div class="row">
      <div class="dynRectangle1" id="dynRectangle1" style="background-color:red;"></div>
    </div>
    <div class="row">
      <div class="dynRectangle2" id="dynRectangle2" style="background-color:green;"></div>
    </div>
  </body>

</html>

Have fun...

Edit: The above will run in your browser and demonstrate the red and green bars you are after

  <div class="dynRectangle1" id="dynRectangle1" style="background-color:red";"></div>

error

exit status 1
missing terminating " character

Hmm, odd that it runs in chrome "background-color:red;"> errant double quote, try now

@indev2 - Your post #12 runs correctly on Firefox on Windows 11.

Works here too (online HTML): W3Schools Tryit Editor

@tom321 - You could clean this up by using tables.

    <h3>
      0V &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;      &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
      &emsp;&emsp;&emsp;&emsp;&emsp;&5 &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
      &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; &emsp;&emsp; 3.5V
    </h3>

yes it is, but post #12 is giving compilation error

I understand.

Where you see the "quote" error, prefix it with an "escape" character: "\" (note: I needed to put two "\" characters in this post to make one show up... the first one being the "escape" character.

expected unqualified-id before '<' token

Have you properly wrapped the markup as originally in webpage.h?
The compiler should not be concerned with the content of a rawliteral.

And at the end

If adding the "escape" caused this... remove the "escape."

Sometimes a line-break sneaks into the code, breaking up the string between the double-quotes. Maximize your window to verify the text goes from the left border to the right border without a line-break.