Is it possible to call a function from an html file from a seperate JavaScript file? Currently I am trying to execute a function based on when a checkbox changes it's status.
The html looks something like this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="/style.css" type="text/css">
<title>Title</title>
</head>
<body>
<div class="grid-container">
<div class="grid-1">
<div class="sidebar">
<h2>Item</h2>
<input type="checkbox" id="item1" onchange="changeVis()" checked /> item1 <br />
<input type="checkbox" id="item2" onchange="changeVis()" /> item2 <br />
</div>
</div>
</div>
<h2>Header 2</h2>
<div id="map" class="map"></div>
<script src="./index.js"></script>
<script type="text/javascript">
wangJangler();
</script>
<script>
function changeVis() {
console.log("hello world")
wangJangler()
}
</script>
</body>
</html>
and the listeners and function wangJangler is in a seperate .js file and looks like this
document.getElementById("item1").addEventListener("change", changeVis());
document.getElementById("item2").addEventListener("change", changeVis());
function wangJangler() {
console.log("WANGJANGLER");
}
The reason the function changeVis is in the html was for testing purposes. Ideally it would also be in the seperate javascript file. Is this even possible? Any help is appreciated. And apologies is the code is horrendously amateurish.