document.getElementByClassName Not working

Hi everyone
Attached is my code where I want background image of a div to be selected by js:

<!DOCTYPE html>

<html lang = "en">
<head>
  <title>to-forum.html</title>
  <meta charset="utf-8" />
  <style>
  .logo 
  {
    width:1200px;
    height:300px;
    margin:auto;
    display: block; 
    background-repeat:no-repeat;
    position:relative;
    border-radius: 25px;
  }
  </style>
    <script>
      function ChangeImage(param)
      { 
        switch(param)
        {
          case '1':
            return("images/greensilver.gif");
            break;
          case'2':				
            return("images/greensilver1.gif");
            break;
        }
      }
    </script>
</head>

<body>
  <div id="logo"> 
    <script>
      document.getElementByClassName("logo").style.backgroundImage = "url('" + ChangeImage('1') + "')";
    </script>
  </div>
</body>
</html>

The image dont show ! Can you help me the code?

Check your browsers dev console, it should log an error in your code. You also try to select elements by class, but the div element does not have the class logo.

in the html tag, id=“something” , in the js, it must by get element by id, not by class.

Sponsor our Newsletter | Privacy Policy | Terms of Service