Ho spinto <form>
HTML file, file JS, e quindi addEventListener per questo, ma una forma di errore risulta:
Uncaught TypeError: Impossibile leggere la proprietà è null (lettura 'addEventListener').
Presumo che è a causa di questo file JS è collegato direttamente al file HTML che significa che il JS potrebbe essere caricato prima <form>
.
Qualcuno può dirmi come risolvere questo problema?
Il JS codici sono i seguenti:
// 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()
})