1

In my HTML file I have a div with id="list". Now I want to display a string from my javascript in my html. page. but nothning happen. In my html file, i've imported the srcipt file. Here's how it looks in my script file:

var namesArray = ["lars", "bo", "ib", "peter", "jan", "frederik"];

var list = namesArray.map(name=>"<li>"+name+"</li>");
var listAsStr ="<ul>" + list.join("") + "<ul>";
document.getElementById("list").innerHTML = listAsStr;
7
  • 1
    What is document.getElementById("list")? Missing / at closing <ul> Commented Feb 12, 2019 at 20:45
  • Is your script in the head of the document? If so, move it to be the last HTML element on the page before the body ends i.e. <script>...</script></body></html> Commented Feb 12, 2019 at 20:47
  • did you wrap your code inside DOMContentLoaded? Commented Feb 12, 2019 at 20:47
  • my html file and javascript file are in two different file. gaetanM. No i haven't why should I need to do that, usually I don't need to do that. Commented Feb 12, 2019 at 20:52
  • 2
    Questions seeking help with ("why isn't/how to make this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. See: How to create a minimal reproducible example. Commented Feb 12, 2019 at 21:00

4 Answers 4

3

Whenever you're targeting DOM elements (i.e you want to use document.getElementById("my-element") or similar) you need to first check if the document has loaded.

You can do this in either of the following ways:

window.onload = function(){
  //Now that the window has loaded we can target DOM elements here
}

OR

document.addEventListener('DOMContentLoaded', function () {
  //Now that the contents of the DOM have loaded we can target DOM elements here
});

So a full example (putting your script code in an external file i.e list.js) would look like this:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8">
<title>My list website</title>
<script src="list.js"></script>
</head>
<body>
  <div id="list"></div>
</body>
</html>

list.js

window.onload = function(){
    //We use window.onload to check the window has loaded so we can target DOM elements
    var namesArray = ["lars", "bo", "ib", "peter", "jan", "frederik"];
    var list = namesArray.map(name=>"<li>"+name+"</li>");
    var listAsStr ="<ul>" + list.join("") + "<ul>";
    document.getElementById("list").innerHTML = listAsStr;
}
Sign up to request clarification or add additional context in comments.

Comments

2

place your code in this and it will work

window.onload = function() {}

Comments

0

You need to put the JavaScript code after your dom and also wrapped with Script tag.

Example: This will work since we rendered the HTML first and then executed the js into it.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>This Will WorK</title>
</head>
<body>
	
	<div id="list" ></div>

	<script>
		var namesArray = ["lars", "bo", "ib", "peter", "jan", "frederik"];

		var list = namesArray.map(name=>"<li>"+name+"</li>");
		var listAsStr ="<ul>" + list.join("") + "<ul>";

		document.getElementById("list").innerHTML = listAsStr;
	</script>
</body>
</html>

But this will NOT work since the JavaScript is being executed before dom rendered. Also this will probably throw an error Cannot set property 'innerHTML' of null" because getElementById will not be able to find the associated dom.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>This Will Not WorK</title>
</head>
<body>
	
	<script>
		var namesArray = ["lars", "bo", "ib", "peter", "jan", "frederik"];

		var list = namesArray.map(name=>"<li>"+name+"</li>");
		var listAsStr ="<ul>" + list.join("") + "<ul>";

		document.getElementById("list").innerHTML = listAsStr;
	</script>
  
	<div id="list" ></div>

</body>
</html>

5 Comments

lfty what if i moved the javascript to a specific file call list.js. and then in my html file directed to my script at the end of the body like this <script src="list"></script>. why didn't it work???
If you wrap your script in window.onload you can put it anywhere. it will check if the window has loading before targeting the DOM elements.
Thanks everyone. it seems everything works fine now, even when the html and script are separated in 2 files!!!
@JohnnyVan it should also work if you link correctly. Maybe you forgot to add type="text/javascript" in your script tag ? Btw if that doesn't work, try the onload method showed by Sarah
@JohnnyVan That's great :)
0

Here is a succinct way to do it with template strings. Just wrap everything in a function you assign to window.onload:

<script>
  window.onload = () => {
    const namesArray = ['lars', 'bo', 'ib', 'peter', 'jan', 'frederik'];
    const list = `<ul>${namesArray.map(name => `<li>${name}</li>`).join('')}</ul>`;
    document.getElementById('list').innerHTML = list;
  };
</script>

<div id="list"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.