Answer the question
In order to leave comments, you need to log in
Why doesn't HTML see the js function?
I added a form and a button to the html, by clicking on which the function from the script should be called.
If I write only a function in this script, then everything works, but if I add imports there, it already writes that the function is not used anywhere and, accordingly, the script does not work.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="./get_input_data.js"></script>
</head>
<body>
<h1>Send message</h1>
<form onsubmit="get_input_data()">
<input type="text" id="data">
<button type="submit" >send</button>
</form>
</body>
</html>
/*import {parameters} from "./Parameters";
import {spam_protection} from "./client";*/
function get_input_data()
{
let text = document.getElementById("data");
alert(text.value);
}
Answer the question
In order to leave comments, you need to log in
If I'm not mistaken, onsubmit only works with scripts written in the same html file inside the script tag. It does not work with external files. In any case, submitting from html is so-so practice. If you still write js in external files, then submit it there as well. And to access specific form values, use the name attributes:
<form id="form">
<input type="text" name="textField">
<button type="submit" >send</button>
</form>
const form = document.querySelector('#form')
function submitForm(event) {
event.preventDefault()
console.log(event.target.textField.value)
}
form.addEventListener('submit', submitForm)
Take it to window
window.get_input_data = function ()
{
let text = document.getElementById("data");
alert(text.value);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question