jQuery .prev()

jQuery .prev() gets the immediately preceding sibling of each element in the set of matched elements; Also check jQuery .next();

js:

$('.target').prev()
    .css('background-color', 'yellow')
    .append(' <span class="label label-info">prev before .target</span>');


$('li').prev('.target')
    .css('background-color', 'yellow')
    .append(' <span class="label">prev with .target class</span>');​

html:

<ul>
<li>list item
<ul>
<li>list item</li>
<li class="target">list item with target class</li>
<li>list item</li>
<li>list item</li>
</ul>
</li>
<li>list item</li>
<li class="target">list item with target class</li>
<li>list item</li>
<li>list item</li>
</ul>​

Leave a Comment