title: Write a jQuery Plugin that return values
body: <p>I'm writing a jQuery plugin that stores some data in some cases.</p>
  
  <p>I'd like to write it in a very flexible way, where I'll can change an input parameter to obtain some value that were stored by the plugin.</p>
  
  <p><strong>Explanation:</strong></p>
  
  <p>When I call <code>$("#any").myPlugin()</code>, my plugin initializes creating a <code>div</code> and some <code>a</code> inside.
  Clicking on an <code>a</code> will store it <code>.index()</code> using the <code>.data()</code> method.
  If I call <code>$("#any").myPlugin("getSelection")</code> then I would like to get the value stored with <code>.data()</code>.</p>
  
  <p><strong>What I'd tried:</strong></p>
  
  <pre><code>(function ($) {
      $.fn.myPlugin = function (action) {
          if (action == null) action = "initialize";
  
          return this.each(function ($this) {
              $this = $(this);
  
              if (action == "initialize") {
                  $this.html('&lt;div&gt;&lt;/div&gt;');
                  var div = $("div", $this);
  
                  div.append('&lt;a&gt;A&lt;/a&gt;').append('&lt;a&gt;B&lt;/a&gt;').append('&lt;a&gt;C&lt;/a&gt;');
  
                  div.children("a").each(function (i) {
                      $(this).click(function (event) {
                          // Here I store the index.
                          $this.data($(this).index());
                          event.preventDefault();
                          return false;
                      });
                  });
  
                  return $this;
              } else if (action == "getSelection") {
                  // With this action, I tried to get the stored value.
                  return $this.data("selectedValue");
              }
          });
      };
  })(jQuery);
  </code></pre>
  
  <p>Simple call to create the elements:</p>
  
  <pre><code>$("#someElement").myPlugin();
  </code></pre>
  
  <p>And here I'd tried to get the index, without sucess:</p>
  
  <pre><code>alert($("#someElement").myPlugin("getSelection"));
  </code></pre>
  
  <p>So, is possible to do what I'm trying?</p>