Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

30 lines (30 sloc) 1.331 kb
<?xml version="1.0"?>
<entry type="method" name="first" return="jQuery">
<title>.first()</title>
<signature>
<added>1.4</added>
</signature>
<desc>Reduce the set of matched elements to the first in the set.</desc>
<longdesc>[<p>Given a jQuery object that represents a set of DOM elements, the <code>.first()</code> method constructs a new jQuery object from the first matching element.</p>
<p>Consider a page with a simple list on it:</p>
<pre><code>
&lt;ul&gt;
&lt;li&gt;list item 1&lt;/li&gt;
&lt;li&gt;list item 2&lt;/li&gt;
&lt;li&gt;list item 3&lt;/li&gt;
&lt;li&gt;list item 4&lt;/li&gt;
&lt;li&gt;list item 5&lt;/li&gt;
&lt;/ul&gt;
</code></pre>
<p>We can apply this method to the set of list items:</p>
<pre><code>$('li').first().css('background-color', 'red');</code></pre>
<p>The result of this call is a red background for the first item.</p></longdesc>
<example>
<desc>Highlight the first span in a paragraph.</desc>
<css><![CDATA[.highlight{background-color: yellow}]]></css>
<code><![CDATA[$("p span").first().addClass('highlight');]]></code>
<html><![CDATA[<p><span>Look:</span> <span>This is some text in a paragraph.</span> <span>This is a note about it.</span></p>]]></html>
</example>
<category slug="traversing/filtering"/>
<category slug="version/1.4"/>
</entry>
Jump to Line
Something went wrong with that request. Please try again.