console.log()
only displays in the developer console of the browser. It does not display on the web page itself.
Your code is not feeding a new line because \n
only shows in the source code not on the page. To display a new line in HTML on the page you need to use a <br>
tag or use other form of spacing.
So, instead of:
document.write('hello World\nThis is me');
You could use:
document.write('hello World<br>This is me');
However, rather than using document.write(), you may prefer to write to a specific element in the page. Below I give an element an id
of data
and then use the JavaScript code to write to this element.
<!DOCTYPE html>
<html>
<body>
<div id="data">You can put text here or leave it blank. It will be replaced.</div>
<script>
document.getElementById("data").innerHTML = "Hello world<br>This is me";
</script>
</body>
</html>
Notice also, I need to place the document.getElementByID("data")
script after the div is created. If I place it before it will not be able to find it. The script code is therefore placed at the end of the <body>
section. There are better ways to do this (such as placing JavaScript code in an external file and using defer
), but for your purposes this should work quite well.
` tag or wrap in pre – Patrick Evans Sep 17 '18 at 02:33