How to connect JS to HTML


#1



Hello, I am trying to practice on my computer and need to visualise on an HTML file what the JS does, though I can't get this code right to display my JS exercises. What am I doing wrong? Thanks in advance


<!DOCTYPE html>
<html>
<body>
    
<button onclick="myFunction()">Try it</button>
    
<p id="demo"></p>

<script>
function myFunction()
var text="";
for (var i = 5; i < 11; i = i + 1){
	console.log(i);
}
    document.getElementById("demo").innerHTML = text;

</script>

</body>
</html>


#2

your function is missing its curly brackets:

function myFunction() /* missing { to mark start of the function*/
var text="";
for (var i = 5; i < 11; i = i + 1){
	console.log(i);
}
    document.getElementById("demo").innerHTML = text;
/* missing } to mark end of the function */

Also, your text variable is empty, nothing will show in html, give it some text:

var text="test";

#3

Thanks for the very fast reply.
If I add "test" it'll just print test and still won't display the string from 5 to 10.


#4

console.log will show in the console unit, which we generally use for debugging, you can't use console.log if you want to display on a webpage