i want to log some value onto console on clicking a button by reading the function from scripts.js file from index.html file.
below is my code,
within index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<link rel="stylesheet" href="src/styles.css">
<script src="src/scripts.js"></script>
</head>
<body>
<button onClick="data()">click</button>
</body>
</html>
within scripts.js file
function data() {
const data = "3";
console.log('data');
}
in doing this, i get error like below,
"uncaught reference error: data is not defined at HTMLButtonElement.onclick"
not sure what is causing the problem. could someone help me with this. thanks.
EDIT:
in the source tab i see this
in the networks tab i see scripts.13aae5c5.js file being called.
EDIT2:
i have tried to create a div with id message and change its content with js code in scripts.js file and it works.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="src/styles.css">
</head>
<body>
<h2>here i am </h2>
<div id="message"></div>
<script src="src/scripts.js"></script>
<button onclick="data">click</button>
</body>
in scripts.js file
function data() {
console.log('data');
}
document.getElementById('message').innerText = "Hello World!";
it displays hello world message but on clicking button it says uncaught reference error data not defined