简体   繁体   中英

Could someone help me with this javascript code

Hello i was learning Javascript from 10 day of javascript videos and in last day it still runs error: Uncaught TypeError: Cannot read property 'insertAdjacentHTML' of null

could someone help me

let ourForm = document.getElementById('ourForm')
let ourField = document.getElementById('ourField')
let ourList = document.getElementById('ourList')

ourForm.addEventListener('submit', (e) => {
    e.preventDefault()
    createItem(ourField.value)
})

function createItem(x) {
    ourList.insertAdjacentHTML('beforeend', x)
}

This will be because the element with an id of ourList does not exist. Double check you have an element in your HTML with this id.

Please recheck your HTML code. It looks like you missed the id ourList somewhere. The error means that your javascript was unable to find the element with the given id.

If you check your HTML code, there must be an element with an id of "ourList" for your code to work. Since the error says that an object with that id currently returns a null, something is incorrect. You most likely either misspelled it, or forgot to add the id to the corresponding HTML element.

You do this by writing:

<ul id="ourList"> ... </ul>

where ul is your element type. ( ul , ol , div ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM