title: Check if getElementsByTagName() contains class attribute | Vanilla JavaScript
body: <p>I have been trying to figure out how to determine whether an element found by its tag name contains a certain class.</p>
  
  <p>I have tried the following but it doesn't work and I don't see why not:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;style&gt;
      .mystyle {
          width: 300px;
          height: 50px;
          background-color: coral;
          color: white;
          font-size: 25px;
      }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
    &lt;p&gt;Click the button to add the "mystyle" class to DIV.&lt;/p&gt;
  
    &lt;button onclick="myFunction()"&gt;Try it&lt;/button&gt;
  
    &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The classList property is not supported in Internet Explorer 9 and earlier versions.&lt;/p&gt;
  
    &lt;div id="myDIV"&gt;I am a DIV element&lt;/div&gt;
  
    &lt;table&gt;
      &lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;/tr&gt;
      &lt;tr&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;/tr&gt;
    &lt;/table&gt;
  
    &lt;p id="demo"&gt;&lt;p&gt;
  
  &lt;script&gt;
      function myFunction() {
        var x = document.getElementsByTagName("tr").classList.contains("mystyle");
        document.getElementById("demo").innerHTML = x;
      }
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The above code should print <code>false</code> in the <code>&lt;p id="demo"&gt;&lt;p&gt;</code> element but does not and throws the following error:</p>
  
  <blockquote>
    <p>TypeError: undefined is not an object (evaluating 'document.getElementsByTagName("tr").classList.contains')</p>
  </blockquote>
  
  <p>Does anyone know how to determine whether an element found by its tag name contains a certain class?</p>