jquery

Looking for jQuery find(..) method that includes the current node


The jQuery find(..) traversal method doesn't include the current node - it starts with the children of the current node. What is the best way to call a find operation that includes the current node in its matching algorithm? Looking through the docs nothing immediately jumps out at me.


Solution

  • For jQuery 1.8 and up, you can use .addBack(). It takes a selector so you don't need to filter the result:

    object.find('selector').addBack('selector')
    

    Prior to jQuery 1.8 you were stuck with .andSelf(), (now deprecated and removed) which then needed filtering:

    object.find('selector').andSelf().filter('selector')