I have a problem in my javascript file, using Visual Studio code. I have a sample HTML file.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
<p> hello with JSON</p>
<button id="btn">click me!</button>
</body>
</html>
I'm trying to add an eventListener to the button I added
const btn = document.getElementById("btn");
btn.addEventListener("click", getData)
function getData(){
fetch('https://jsonplaceholder.typicode.com/users')
.then((res) => { return res.json() })
.then((data) => {
console.log(data);
});
}
Why is this issue happening?