title: Trying to write a function to write less code
body: <p>I have a lot of &quot;on change&quot; handlers to store values in localStorage. Now I'm trying to shorten my code. This is one;</p>
  <pre><code>    $('input[name^=&quot;tornfalksgrand2&quot;]').on('change', function () {
  
        let allItems = $('input[name^=&quot;tornfalksgrand2&quot;]:checked').map(function(){
          return this.value;
        }).get();
        localStorage['tornfalksgrand2'] = JSON.stringify(allItems)
  
      });
  </code></pre>
  <p>So far I tried this function;</p>
  <pre><code>    function addDataToLocalStorage($input, $inputChecked, localStorageName ) {
        $input.on('change', function () {
          let allItems = $inputChecked.map(function(){
            return this.value;
          }).get();
          localStorage[localStorageName] = JSON.stringify(allItems)
        });
     }
  </code></pre>
  <p>And then run the function with this;</p>
  <pre><code>addDataToLocalStorage($('input[name^=&quot;tornfalksgrand2&quot;]'), $('input[name^=&quot;tornfalksgrand2&quot;]:checked'), 'tornfalksgrand2');
  </code></pre>
  <p>This stores an empty array in localStorage and not the value I want.
  Any idea anyone?</p>