Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Simple clear or reset form on JavaScript

Can anyone tell me what's wrong with my code I keep getting error:main.js:43 Uncaught TypeError: Cannot read property 'reset' of null at clearIt (main.js:43) at HTMLInputElement.onclick (index.html:59)

I want to reset the form with a clear button.

This is the JavaScript: function getJson(){ let city = document.querySelector('#city').value; let zip = document.querySelector('#zip').value; document.querySelector('#city-title')

fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city}&zip=${zip}&appid=186a445ce8d24b03d24996a4ab982100&units=imperial`)
.then(response => response.json())
.then(rawData =>{
    console.log(rawData)

    let high = `${rawData.main.temp_max}\u00B0F`
    let display_high = document.createElement('w1')
    display_high.innerHTML = high
    document.querySelector('#high').append(display_high)


    let low = `${rawData.main.temp_min}\u00B0F`
    let display_low = document.createElement('w1')
    display_low.innerHTML = low
    document.querySelector('#low').append(display_low)


    let forecast = rawData.weather[0].main
    let display_forecast = document.createElement('w1')
    display_forecast.innerHTML = forecast
    document.querySelector('#forecast').append(display_forecast)

    let humidity = `${rawData.main.humidity}%`
    let display_humidity = document.createElement('w1')
    display_humidity.innerHTML = humidity
    document.querySelector('#humidity').append(display_humidity)
})

} function displayCity(){ let city = document.querySelector('#city').value; let display_city = document.createElement('w2') display_city.innerHTML = city.charAt(0).toUpperCase()+ city.slice(1) document.querySelector('#city-title').append(display_city)

} var weather = 0; function clearIt(){ document.querySelector ("getJson").reset(); weather = 0; }

This is the html: <input type="button" value="Clear" onclick="clearIt()">

2 Answers

Steven Parker
Steven Parker
231,007 Points

The HTML code for the form is not shown here, but since querySelector("getJson") would look for a tag name of "getJson", and since that's not a valid HTML tag name, that's probably the issue or at least part of it.

But you don't need JavaScript to implement a reset button, since that's a built-in feature of a form. Just include this somewhere inside the form:

  <input type="reset">

Hey Steven, I couldn't figure it out so I just left it as is. But thank you so much for trying to help :)!

Steven Parker
Steven Parker
231,007 Points

If you decide you'd like to know, post the complete code or a link to a workspace snapshot for a detailed answer.

Ok thank you!