Am împins-o <form>
pentru fișierul HTML, JS fișiere, și apoi addEventListener la această formă, dar o eroare dovedește:
Neprins TypeError: nu se Poate citi proprietăți de nul (citit addEventListener').
Presupun că asta se datorează faptului că acest fișier JS este direct legat de fișierul HTML care înseamnă JS ar putea fi încărcate înainte de <form>
.
Poate cineva te rog spune-mi cum de a rezolva acest lucru?
JS codurile de mai jos:
// 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()
})