title: Updating a SharePoint 2013 list with JavaScript, works but not quite
body: <p>I am using Script Editor web part to insert data into a SharePoint 2013 list with JavaScript. When I use the following code in the Script Editor, I get the result I want every time the page loads:</p>
  
  <pre><code>&lt;script&gt;
      SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {});
      var context = SP.ClientContext.get_current();
      var list = context.get_web().get_lists().getByTitle('my_list');
      var itemInfo = new SP.ListItemCreationInformation();
  
      this.oListItem = list.addItem(itemInfo);
  
      oListItem.set_item('Title', 'My title testing');
      oListItem.update();
      context.load(oListItem);
  &lt;/script&gt;
  </code></pre>
  
  <p>After the page loads, I get the new item in <em>my_list</em> with <em>My title testing</em> in the Title column.</p>
  
  <p>However, I want this to happen when a user clicks on a button. So I created a button with the <em>onclick</em> property pointing to a function I've put the above code in, but it doesn't work - nothing happens. This is the contents of the Script Editor after I've put the button and wrapped the script inside a function:</p>
  
  <pre><code>&lt;html&gt;
      &lt;button type="button" onclick="addToList()"&gt;Submit&lt;/button&gt;
  &lt;/html&gt;
  
  &lt;script&gt;
      function addToList() {
      SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {});
      var context = SP.ClientContext.get_current();
      var list = context.get_web().get_lists().getByTitle('my_list');
      var itemInfo = new SP.ListItemCreationInformation();
  
      this.oListItem = list.addItem(itemInfo);
  
      oListItem.set_item('Title', 'My title testing');
      oListItem.update();
      context.load(oListItem);
     }
  &lt;/script&gt;
  </code></pre>
  
  <p>I can't figure out what am I doing wrong...</p>
  
  <p>Thanks a lot!</p>