jQuery Tutorial Tutorials - jQuery .prev() method

jQuery .prev() method

The .prev() method in jQuery is used to get the immediately preceding sibling element.

jQuery .prev() method Syntax

.prev()
.prev(selector)

jQuery .prev() method Example

example

Find the very preceding sibling of Item 3. optionaly filtered by a selector.

HTML

<ul>
    <li class="one">item1</li>
    <li>item2</li>
    <li class="third">item3</li>
    <li>item4</li>
    <li>item5</li>
</ul>

jQuery

$(".third").prev();
$(".third").prev('li');
$(".third").prev('.one'); //empty

Result

[<li>item2</li>]
[<li>item2</li>]
[] 

Try now

Date:2019-08-26 14:10:44 From:www.Lautturi.com author:Lautturi