Som tlačil <form>
HTML súbor JS súbor, a potom addEventListener k tejto forme, ale chybové ukáže:
Uncaught TypeError: nemôžem čítať vlastnosti null (čítanie "addEventListener').
Predpokladám, že je to preto, že tento JS súbor je prepojené priamo s HTML súbor, ktorý znamená, JS môže byť vložený pred <form>
.
Môže mi niekto prosím, povedzte mi, ako vyriešiť tento problém?
JS kódy sú uvedené nižšie:
// 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()
})