title: How to re-evaluate or refresh jquery selector
body: <p>I have a selector stored in a variable. The content in the page changes dynamically, the problem I'm facing was when the content was removed then the cached selector didn't work. How can I re-evaluate or refresh the selector?
  <a href="https://jsfiddle.net/qehdvedd/" rel="nofollow noreferrer">https://jsfiddle.net/qehdvedd/</a></p>
  
  <pre><code>var selector;
  function Remove(){  
  selector.refresh();
    selector.remove();
  }
  
  function Add(){
      $("#div1").append('&lt;div class="div2"&gt;New content&lt;/div&gt;');
  }
  
  $(document).ready(function () {
      selector = $(".div2");
  });
  
  $.fn.refresh = function() {
      return $(this.selector);
  };
  </code></pre>
  
  <p>I also referenced (<a href="https://stackoverflow.com/questions/11868899/how-can-i-refresh-a-stored-and-snapshotted-jquery-selector-variable">How can I refresh a stored and snapshotted jquery selector variable</a>) but it does not work.
  I'm aware of the alternative solutions like storing the selector name as a string and calling it with jquery.</p>
  
  <p><strong>Edit</strong>:
  I will tell the exact scenario. I'm working on a spa website, in that if I clicked an element and go to next page in the spa. Then if I pressed Esc or back button I need to focus the last selected item also need to add a class to show which is selected. I will store the selector in a variable to know the last selected item when the element is clicked. And if the Esc key is pressed then I will focus on the variable which stored the last selected item. But due to spa the contents are removed and added dynamically. So this solution doesn't seem to work. I can't use the alternative solutions like storing the selector name as a string and calling it with jquery. Because there are too many items present on the page, so adding every item with Id is not practical.
  Sample: <a href="http://jsfiddle.net/2v78hmb5/" rel="nofollow noreferrer">http://jsfiddle.net/2v78hmb5/</a></p>