O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting the Closest Elements

$("p.menu").closest("div");   //selects the closest <div> ancestor for <p>   //elements that have class="menu"

The .closest(selector,[context] or object or element) method returns a jQuery object representing the first element that matches the argument that is passed in. The argument can be a selector, a selector with context of where to begin searching, a jQuery object, or a DOM object. The search begins at the current set of elements and then searches ancestors.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required