jQuery - parentsUntil()

The parentsUntil() method returns all ancestor elements between two given arguments. The following example returns all ancestor elements between a demo4 and demo2.

div 1
div 2
div 3
div 4

 


$("#btnDemo").click(function ()
{
    $("#demo4").parentsUntil("#demo2").css({ "background-color": "red" });
});