document.getElementByClassName Not working


#1

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?


#2

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.


#3

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