title: Generate random background color by clicking on link
body: <p>In my JavaScript program I have randomly generated color for background by using Math.random(). Upon loading the page it's automatically generating random color. I have also created link CLICK HERE. When user click on link it should also generate background color for that I used Onclick function and added code to randomly generate color but when I click on link it's not generating. Every time I click on link it should generate random color. Can anyone correct me ?</p>
  
  <p>code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;style type="text/css"&gt;
          #para{
              text-align: center;
              font-size: 20px;
              color: white;
          }
  
          #link{
              text-align: center;
              font-size: 20px;
              }
  
  
      &lt;/style&gt;
      &lt;title&gt;lab13&lt;/title&gt;
      &lt;script type="text/javascript"&gt;
  
          document.write('&lt;br&gt;'); document.write('&lt;br&gt;');
          document.write('&lt;p id = "para"&gt;' + 'BACKGROUND-COLOR IS RANDOMLY GENERATED' + '&lt;/p&gt;');
  
  
  
          function random_bg_color(){
  
          var rgbcolor;
          red = Math.floor(Math.random() * 250 + 0 );
          green = Math.floor(Math.random() * 250 + 0);
          blue = Math.floor(Math.random() * 250 + 0);
  
          rgbColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
          document.body.style.background = rgbColor;
  
          document.write('&lt;p id = "para"&gt;RGB(' + red + ', ' + green +  ', ' + blue + ')&lt;/p&gt;');
  
          red = ("0" + red.toString(16)).substr(-2).toUpperCase();
          green = ("0" + green.toString(16)).substr(-2).toUpperCase();
          blue = ("0" + blue.toString(16)).substr(-2).toUpperCase();
  
          document.write("&lt;p id = 'para'&gt;HEX: #" + red + '' + green + '' + blue + '&lt;/p&gt;');
  
          }
  
          random_bg_color();
  
          function randomize() { 
          random_bg_color();
          document.body.style.background = rgbColor;
          }
  
  
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;a id="a1" href="#" onclick="randomize()"&gt; CLICK TO RANDOM &lt;/a&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>output:</p>
  
  <p><a href="https://i.stack.imgur.com/amJ3m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/amJ3m.png" alt="enter image description here"></a></p>