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

Ben Masel
Ben Masel
2,004 Points

Instant Update

Dear All,

I am making a google chrome extension. I have a settings menu where you change something then you have to reload the page. I was wondering if I could make the javascript run when changed without having to reload the whole webpage. it would be awesome.

The example code is weather on/off switch. You have to reload once the checkbox value is changed from checked to unchecked visa versa. It is saved with local storage etc.

Here is the html code

      <div id="weatherOptions">

        <label class="sideAccent">Weather Off</label><input type="checkbox" id="weatherToggle"><label class="sideAccent">Weather On</label>

      </div>

Here is the js code

// onsubmitmodifier() is what happens when reloaded
function onsubmitmodifier() {
   localStorage.weather = document.getElementById("weatherToggle").checked;
}

//Weather Enabled?
if (localStorage.weather == "false") {
  document.getElementById("weatherOpen").remove();
  document.getElementById("weatherToggle").checked = false;
} else {
  document.getElementById("weatherToggle").checked = true;
}

So how do I make this website, as soon as I change the value of the checkbox, it will change the value of localStorage.weather without reloading the page?

1 Answer

Steven Parker
Steven Parker
231,007 Points

Just call "onsubmitmodifier" again after you've changed the element state.