-2

This is my javascript code(create.js):

var stuff = document.querySelector(".stuff");


var item = document.createElement('div');
item.className = 'item';

stuff.appendChild(item);

and this is my HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sound Create</title>
    <script src="create.js"></script>
</head>
<body>
    <div class="stuff">

    </div>

</body>
</html>

When I go on Developer Tools, no code is added. Thank you!

1 Answers1

-1

When you call "getElementsByClassName", you will get a LIST of HTML elements, even if it contains only one element. So the right way is:

document.getElementsByClassName("stuff")[0];

Another way is using id. It's like , in js file, use document.getElementById('staff'). It returns the element directly, because id is always unique.

Chris.Z
  • 51
  • 5
  • 1
    Thanks for trying to help, but it is giving this error:Uncaught TypeError: Cannot read property 'appendChild' of null at create.js:6 – themiscoding May 01 '20 at 13:54