Despite several other solutions, I have yet to find one that actually solves my problem. I have an HTML file containing a header with
<script type="text/javascript" src="./js/throwaway.js"></script>
and in the body there is a button like so:
<button onclick="basicAlert()"></button>
and I have the Javascript function basicAlert():
function basicAlert() {
alert("this is a test to see if basicAlert runs");
}
I've been stuck on this for two days now to no avail, but I cannot get the onclick function to execute basicAlert() when the function is in the external file. I can have a script in the HTML holding the function, and it will run flawlessly, but when the function is in the external file, it does not run. Attempted solutions included trying onsubmit (it's a search bar), removing 'type="text/javascript"' (since default is JS anyways), and moving the script src to the different sections of the file, headers, body, at the end, etc. making the path things like "/js/throwaway.js", "../js/throwaway", every combination I could think would make sense. The current file set-up is a Node app.js that references from folder public, which contains the HTML doc and a folder "js" which contains the throwaway program. And yes the file location is correct in relation to the HTML, where it's being referenced. If I could just have the function inside the HTML, I would, but there's going to be several functions, so I don't want to clutter the HTML file.
I'm very new to javascript and HTML, so I would appreciate any assistance. Thanks in advance.
EDIT: someone asked for full relevant code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Spotlight</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="./js/spotifyFunctions.js"></script>
</head>
<body>
<input type="text" class="form-control" id="songSearch" placeholder="Enter Song or Playlist...">
<button id="search" onclick="basicAlert()"><div class="search-icon"><ion-icon name="search"></ion-icon></div></button>
<script src="link"></script> <!-- There's about 20 of these before the basic alert -->
<script>
function basicAlert() {
alert("throwaway");
}
</script>
</body>
</html>
EDIT 2: The Javascript file I have may be the culprit for the errors due to its imports, thanks to those who helped me out in here
import Spotify from "spotify-web-api-js";
const spotifyApi = new Spotify();
//Several functions utilizing spotify API
function basicAlert() {
alert("this is a test to see if basicAlert runs");
}
console.log("made it");
Throws error in console:
SyntaxError: import declarations may only appear at top level of a module
I can probably figure this one out on my own. Thanks!