Kliknąłem na <form>
w pliku HTML za pomocą pliku JS, a następnie dodać do formularza element listy zdarzeń, ale okazuje się błąd:
Неперехваченная błąd typu: Nie można odczytać właściwości null (czytanie "addEventListener").
Myślę, że jest to związane z tym, że ten plik JS jest bezpośrednio powiązany z plikiem HTML, co oznacza, że JS może być załadowany do <form>
.
Czy ktoś może, proszę mi powiedzieć, jak to rozwiązać?
Kody JS przedstawiono poniżej:
// skip to the input fields
$start.addEventListener('click', function(){
$chooseStory.remove()
const inputs = []
inputs.push(`
<form id="form">
<label>Provide The Following Words</lable>
`)
// assign words of stories to names and placeholders of inputs
// the input will automatically loop for as many as the words are
for (const word of stories[$index.value].words) {
inputs.push(`
<input type="text" name='${word}' placeholder="${word}">
`)}
inputs.push(`
<button type="submit" id="submit"> Read Story </button>
<code id="result"></code>
</form>
`)
const inputsField = inputs.join('')
$container.innerHTML += inputsField
})
// retrieve value of the form
const $form = document.getElementById('form')
$form.addEventListener('submit', function(e){
e.preventDefault()
})