title: How can I simulate a click on d3 tree nodes?
body: <ul>
  <li>I've got an expandable tree like this: <a href="http://jsbin.com/okUxAvE/22/" rel="nofollow noreferrer">http://jsbin.com/okUxAvE/22/</a></li>
  <li><strong>I want to save the state of nodes, so that when you leave the page and come back later, the tree is expanded the way it was when you left</strong>. I asked about that at <a href="https://stackoverflow.com/questions/19200891/save-d3-tree-state-to-localstorage">Save d3 tree state to localStorage</a>, and that part of the puzzle is solved; saving to localStorage works like a charm.</li>
  </ul>
  
  <p>Depending on whether the node is expanded or collapsed, I'm either adding the node ID to localStorage or removing it, like:</p>
  
  <pre><code>var nodeState = JSON.parse(localStorage['openNodes']);
  nodeState.push(d.id);
  localStorage['openNodes'] = JSON.stringify(nodeState);
  </code></pre>
  
  <p>(Apparently, I should change this to dot notation.)</p>
  
  <p>This works well, giving me <code>['3','19']</code> (for example) if nodes 3 and 19 are expanded. If 19 were closed, it is removed from the array. (you can see this by clicking around and then doing <code>localStorage.openNodes</code> in the example's console).</p>
  
  <p>So I have this information and can get it, but the various ways I've tried expanding the nodes programmatically after retrieving the localStorage data seem to be buggy at best. In any case, getting the nodes to expand doesn't seem to be working.</p>
  
  <p>I thought I could compare the <code>d.id</code> to the items in localStorage (there are never many) and simulate a click by calling <code>click(d)</code>, but no dice. Something like:</p>
  
  <pre><code>if (localStorage['openNodes']) {
  
    var savedState = JSON.parse(localStorage['openNodes']);
  
    for(var i = 0; i &lt; savedState.length; i++) {
      if (d.id === savedState[i]) {
        // simulate click/call click()/something else
      }
  }
  </code></pre>
  
  <p>How can I do this? Should I even be trying to call <code>click()</code> at all? Please don't assume extensive JavaScript knowledge on my part. I'm doing my best, but I'm in learning territory.</p>