I have created a form in Html to post data to a Googlesheets and it is working fine. When I try to seperate the Javascript file and use it inside html file the script is not working.
What might be the problem and please help me out to solve it.
This is my script.js file:
const scriptURL = 'https://script.google.com/macros/s/AKfycbxRJo4gGwmTHCh6jXO2fcICFJQwI1LwEi7dPI_7vbLq8r4Q-6hbzXprJWIvYn6N7JL_Vw/exec'
const form = document.forms['submit-to-google-sheet']
const msg = document.getElementById("msg")
form.addEventListener('submit', e => {
e.preventDefault();
fetch(scriptURL, { method: 'POST', body: new FormData(form)})
.then(response => {
msg.innerHTML = "We will get back to you!"
setTimeout(function(){
msg.innerHTML = ""
},5000)
form.reset()
})
.catch(error => console.error('Error!', error.message))
})
I have tried to use it in Html file with the tag:
<script src="script.js"></script>
If I use it inside html file it is properly working. I want to split the script file apart from my html file and not able to perform the functionality even using it from outside or html file with linking it to script file.