title: Onsen 2.0 - Adding event listener to Ons-Switch with Javascript
body: <p>I fear I am missing something really simple, but I have tried multiple attempts with various errors.  On to the code:</p>
  
  <pre><code>&lt;script&gt;
          document.getElementById("optStats").on("change", function(e) {
      alert("Switch changed!");
    });
      &lt;/script&gt;
  &lt;ons-switch modifier="list-item" id="optStats"&gt;&lt;/ons-switch&gt;
  </code></pre>
  
  <p>So the above does not work.  It generates an error of <code>Uncaught TypeError: Cannot read property of on of null</code>.  So I assumed that I needed to add the function to the onload init function but it still failed with the same error.</p>
  
  <p>I have tried all the options listed here: <a href="https://onsen.io/guide/overview.html#EventHandling" rel="nofollow">https://onsen.io/guide/overview.html#EventHandling</a> without success.  This would include using var instead of id and just about anything else listed that I thought would work.</p>
  
  <p>Edit:  Full code that is not working, but works in codepen without template tag.</p>
  
  <pre><code>&lt;ons-template id="options.html" &gt;
          &lt;ons-page&gt;
              &lt;ons-toolbar&gt;
                  &lt;div class="center"&gt;Options&lt;/div&gt;
              &lt;/ons-toolbar&gt;         
              &lt;ons-list modifier="inset" style="margin-top:10px;"&gt;
                  &lt;ons-list-item&gt;
                    Detailed Stats
                    &lt;ons-switch modifier="list-item" id="optStats"&gt;&lt;/ons-switch&gt;
                  &lt;/ons-list-item&gt;
              &lt;/ons-list&gt;            
          &lt;/ons-page&gt;
      &lt;/ons-template&gt;
  </code></pre>
  
  <p>I am using this in ons.ready() and have tried it outside which results in the same error, <code>cannot read property of addEventListener</code>, as listed above for both.</p>
  
  <pre><code>document.getElementById('optStats').addEventListener('change', function(e) {
        console.log('click', e.target.checked);
      });
  </code></pre>
  
  <p><strong>Last Update I swear:
  FINALLY!!!!  Thank you @Fran-dios!</strong></p>
  
  <p>After working with this, init is not the event you want to use, you definitely want to use show for what I was trying to do.</p>
  
  <pre><code>document.addEventListener("show", function(event){
      if (event.target.id == "pgOptions") { 
          document.getElementById('optStats').addEventListener('change', function(e) {
              localStorage.setItem('useDetailedStats', e.target.checked); 
              useDetailedStats = e.target.checked;
          });
          document.getElementById('optStats').setChecked(useDetailedStats);
        } 
  },false);
  </code></pre>
  
  <p>For whatever reason, when using init on a <code>&lt;ons-tabbar&gt;</code>, whenever you would press the tab, it would cause the switch to change states.  By using show, it does not.  Additionally, when I logged the init action, the page, even though it was not being shown on app startup, was still being logged as being init twice.  Show does not cause this.  Either way, the code that @fran-dios provided is the correct basis to get what I needed and his answer is still correct, I just needed to use a different event.</p>