Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
51 lines (48 sloc) 2.73 KB
<?xml version="1.0"?>
<entry type="method" name="next" return="jQuery">
<argument name="selector" optional="true" type="Selector">
<desc>A string containing a selector expression to match elements against.</desc>
<desc>Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.</desc>
<p>Given a jQuery object that represents a set of DOM elements, the <code>.next()</code> method allows us to search through the immediately following sibling of these elements in the DOM tree and construct a new jQuery object from the matching elements.</p>
<p>The method optionally accepts a selector expression of the same type that we can pass to the <code>$()</code> function. If the immediately following sibling matches the selector, it remains in the newly constructed jQuery object; otherwise, it is excluded.</p>
<p>Consider a page with a simple list on it:</p>
&lt;li&gt;list item 1&lt;/li&gt;
&lt;li&gt;list item 2&lt;/li&gt;
&lt;li class="third-item"&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;
<p>If we begin at the third item, we can find the element which comes just after it:</p>
<pre><code>$('li.third-item').next().css('background-color', 'red');</code></pre>
<p>The result of this call is a red background behind item 4. Since we do not supply a selector expression, this following element is unequivocally included as part of the object. If we had supplied one, the element would be tested for a match before it was included.</p>
<desc>Find the very next sibling of each disabled button and change its text "this button is disabled".</desc>
<code><![CDATA[$("button[disabled]").next().text("this button is disabled");]]></code>
span { color:blue; font-weight:bold; }
button { width:100px; }
<html><![CDATA[<div><button disabled="disabled">First</button> - <span></span></div>
<div><button>Second</button> - <span></span></div>
<div><button disabled="disabled">Third</button> - <span></span></div>]]></html>
<desc>Find the very next sibling of each paragraph. Keep only the ones with a class "selected".</desc>
<code><![CDATA[$("p").next(".selected").css("background", "yellow");]]></code>
<p class="selected">Hello Again</p>
<div><span>And Again</span></div>]]></html>
<category slug="traversing/tree-traversal"/>
<category slug="version/1.0"/>
Jump to Line
Something went wrong with that request. Please try again.