1
    <!DOCTYPE html>
<html>
<head>
<script>
    function myFunction() {
                let exmpale = document.createElement("div");
                exmpale.innerHTML = "aaaaa";
                exmpale.className = "classDiv";
                document.getElementsByClassName("myDIV").appendChild(exmpale);
    }      
</script>
</head>
<body>

<div class="myDIV">
A DIV
</div>

<button onclick="myFunction()">button</button>
</body>
</html>

I don’t know why but when I'm clicking the button it doesn't print out the value inside the innerHTML (aaaaa).

  • 1
    Check out what `document.getElementsByClassName` returns, in the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName). If you only want the first one, add `[0]` at the end, or use [`document.querySelector('.myDIV')`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) instead – blex Jun 24 '21 at 21:36
  • you have a bad perception of the value returned by the method `.getElementsByClassName` – Mister Jojo Jun 24 '21 at 21:38
  • Also, when things "don't work", try checking the browser console log. The error messages in the console will tell you what went wrong. – Terry Jun 24 '21 at 21:39
  • also in your title => `getElementByClassName()` doesn't exist in javascript – Mister Jojo Jun 24 '21 at 21:42

0 Answers0