Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery prev() method

jQuery prev() method

jQuery Traversal Methods jQuery Traversal Methods

Example

Return each with class name "start"

  • the previous sibling element of the:

    $

    (

    document

    )

    .

    ready

    (

    function

    (

    )

    {

    $

    (

    "

    li.start

    "

    )

    .

    prev

    (

    )

    .

    css

    (

    {

    "

    color

    "

    :

    "

    red

    "

    ,

    "

    border

    "

    :

    "

    2px solid red

    "

    }

    )

    ;

    }

    )

    ;

    Result:

    • li (sibling)
    • li (sibling)
    • li (sibling with class name "start")
    • li (sibling)
    • li (sibling)

    Try it »


    Definition and Usage

    The prev() method returns the previous sibling element of the selected element.

    Sibling elements are elements that share the same parent element.

    **Note:** This method returns only one element.

    **DOM Tree:** This method traverses backward along the previous sibling elements of the DOM element.

    Related methods:

    • prevAll() - returns all sibling elements before the selected element
    • prevUntil() - returns all sibling elements before each element between two given arguments

    Syntax

    $(

    selector

    ).prev(

    filter

    )

    Parameters Description
    filter Optional. Specifies a selector expression to narrow down the search for the previous sibling element.

    More examples

    Select every

    the previous sibling element of the
    How to select each
    the previous sibling element of the.

    Narrow down the search
    How to select each

    the previous sibling of the

    .


    jQuery Traversal Methods jQuery Traversal Methods