Jquery selector .each()
<p>Consider the following HTML:</p>

<pre><code>&lt;div id="myfavorites"&gt;
 &lt;p&gt;&lt;button id="saveFav"&gt;Save my favorites&lt;/button&gt; &lt;/p&gt;
 &lt;p&gt;&lt;a href="http://bit.ly/fzgxya"&gt;http://bit.ly/fzgxya&lt;/a&gt; &lt;a class="favlinks" href="#"&gt;(remove me)&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
</code></pre>

<p>When button is pressed, I want to make a json object with all the links.</p>

<pre><code>$('#saveFav').click(function() {
  var toSave = { "searchtext" : $('#searchText').val().trim() };
  var links = {};

  $('#myfavorites p').each(function(index) {
    links[index] = $(this).first('a').attr('href');
  });

  toSave.links = links;
}
</code></pre>

<p>But in $('#myfavorites p').each function, $(this) isn't the <strong>p</strong> element. I am missing something here. How can I iterate in all <strong>p</strong> and find the first <strong>a</strong> element?</p>

<p>And am I construction the object correctly? I mean if I pass it to a php page, will it json_decode correctly?</p>

<p>thanks</p>
