-1

I'm trying to use Document.getElementByClassName, but it isn't working. I've included my code below. I'd appreciate any help.

HTML document:

<!doctype html>
<html>

<head>
  <meta charset=utf-8>
  <title>Day Practice</title>
  <style></style>
</head>

<body>
  <h1 class=myclass> Some text</h1>
</body>
</html>

JavaScript code:

var change = document.getElementByClassName("myclass");
change.innerHTML = "New text";
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Sam Opoku
  • 69
  • 2
  • 8

2 Answers2

6

It's getElementsByClassName Elements

Returns an array-like object of all child elements which have all of the given class names
- Mozilla Developer Network / Document.getElementsByClassName()

Loop through it or use change[0].innerHTML

1

   var change = document.getElementsByClassName("myclass");
   change[0].innerHTML = "New text";
 <h1 class="myclass"> Some text</h1>

2

   var change = document.getElementsByClassName("myclass");

   for (var i = 0; i < change.length; i++) {
     change[i].innerHTML = "New text";
   }
 <h1 class="myclass"> Some text</h1>
JAL
  • 41,701
  • 23
  • 172
  • 300
Nick
  • 3,231
  • 2
  • 28
  • 50
0

Right before the closing body tag (), you want to add a script tag to attach your JavaScript file into the HTML file, so that they're both linked.

This is how it should look:

<head>
 <meta charset=utf-8>
 <title>Day Practice</title>
 <style></style>
</head>

<body>
  <h1 class=myclass> Some text</h1>
  <script src="javascriptfile.js"></script>
</body>

Also, it's "getElementsByClassName"; elements is plural

Ushi
  • 49
  • 1
  • 2
  • 3