Consider following code snippet:

<div>
        <p>Something_1</p>
        <p>Something_2</p>
        <p class="foo">Something_3</p>
    </div>
    <script>
        function ReturnValue(){
            var countElem = $('.foo').prev('p').length
                            +$('.foo').prevAll('p').length
                            +$('.foo').prevUntil('div').length;
            console.log(countElem);
        };    
        ReturnValue();
    </script>
What will be printed to the console?
Explanation
The prev() method returns the previous sibling element of the selected element. The prev() method returns all the previous sibling elements of the selected element. The prevUntil() - returns all previous sibling elements between two given arguments. If both parameters are empty, this method will return all previous sibling elements (same as the prevAll() method).

$('.foo').prev('p').length;          // 1
$('.foo').prevAll('p').length;       // 2
$('.foo').prevUntil('div').length;  // 2 ,because no parameters were passed

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes