/    Sign up×
Community /Pin to ProfileBookmark

Error: Uncaught TypeError: Cannot read property ‘appendChild’ of null

Hi guys, I’m getting the above error when trying to append a ‘div’ element using javascript to an existing ‘div’ element in my HTML and really don’t understand why. I must be missing something very obvious (you may have guessed I’m a beginner). My HTML code is:

`<head>
<title>Etch-a-Sketch Project</title>
<meta>
<link rel=”stylesheet” href=”styles/main.css”>
<script type=”text/javascript” src=”scripts/script.js”></script>
</meta>

<body>
<h1 id=”title”>Etch-a-Sketh Project</h1>

<div id=”container”>
</div>

</body>`

and my JavaScript code is:

`const container = document.querySelector(“#container”);

const div1 = document.createElement(‘div’);
div1.classList.add(“grid”);
div1.textContent=”div1″;
container.appendChild(div1);`

Thank you in advance for any help/guidance.

to post a comment
HTMLJavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@KeverApr 11.2020 — Move your <script> tag to the end of the <body> so all the DOM is loaded when your script executes.
Copy linkTweet thisAlerts:
@Turbo1248authorApr 11.2020 — @Kever#1617308 Hi Kever, my <script> reference is in the <head> section because it links to a separate javascript file. Are you saying I need any DOM related javascript code written in a '<script> element within the actual html file?

Thanks
Copy linkTweet thisAlerts:
@cootheadApr 11.2020 — Hi there Turbo1248,

you appear to have an errant backtick here...

[size=32]container.appendChild(div1);[color=red]`[/color][/size]

...and it is the cause of your woes. :(

_coothead_
Copy linkTweet thisAlerts:
@Turbo1248authorApr 11.2020 — Hi Coothead, WebDeveloper actually added that when I selected 'insert code' - there's one at the beginning of the code I copied into the message too!
Copy linkTweet thisAlerts:
@Turbo1248authorApr 11.2020 — I tried putting the exact same code in a <script> element within the html code, rather than putting it in, and referencing, an external .js file, and it worked fine.

So then, I moved the following code in the original html file '<script type="text/javascript" src="scripts/script.js"></script>', from the <meta> section within the <head> element, to the end of the <body> element within the html file, and it worked fine!

So the code was fine, which leads me to a question for clarification - when referencing an external .js file, should this always be placed in the <body> element rather than within the <head> element? I thought all externall css and .js files etc. were linked to in the <head>..<meta> section. Thanks!
Copy linkTweet thisAlerts:
@NachfolgerApr 11.2020 — @Turbo1248#1617313

I have never heard of using the meta tag like you have. I would even propose that your usage is 100% incorrect. The meta tag exists, but for inline attributes only. I could be wrong, but I’m fairly certain I’m not.

The issue was that the script was loading before the body content was, and the element you were searching for didn’t exist at time of execution. There’s no reason for your JS file to be in the head tag at all, only CSS belongs there.
Copy linkTweet thisAlerts:
@cootheadApr 11.2020 — Hi there Turbo1248,

you need to use [b]3[/b] backticks ...

[b] `` [/b]</B>

...at the start and end of the code.


Alternatively, if you prefer, use this...

<C>
`

...to enclose the code.

**Example:-**

<i>
</i>const container = document.querySelector("#container");

const div1 = document.createElement('div');
div1.classList.add("grid");
div1.textContent="div1";
container.appendChild(div1);


_coothead_
Copy linkTweet thisAlerts:
@Turbo1248authorApr 11.2020 — @Nachfolger#1617314 thanks Nachfolger, and that makes a lot of sense.
Copy linkTweet thisAlerts:
@Turbo1248authorApr 11.2020 — @Kever#1617308 Figured this out now and you were right - many thanks for your help Kever
×

Success!

Help @Turbo1248 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...