jQuery .prev()

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

<script>
$('.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>');</script>
<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 Reply

Your email address will not be published. Required fields are marked *