jQuery - xpath find?

18,308

Solution 1

jQuery used to support very basic XPath, including the example you gave.

$(xml).find("animal/dog/beagle")

EDIT: You're right, they've apparently removed it from the core, so you have to use a "compatibility" plugin.

EDIT: Updated link to xpath plugin XPath Plugin

Solution 2

jQuery supports basic XPath actually, so you can just use find.

Alternatively, use CSS selector syntax. For your particular example, you would use $(xml).find( "animal > dog > beagle" ).text()

Share:
18,308
Steve
Author by

Steve

Updated on June 04, 2022

Comments

  • Steve
    Steve about 2 years

    If you have the xml below in $(xml), you would get droopy using:

    $(xml).find("animal").find("dog").find("beagle").text()
    

    Is there an equivalent way in jQuery to use xpath like

    $(xml).xpathfind("/animal/dog/beagle").text()?
    
    <animal>
        <dog>
            <beagle>
                droopy
            </beagle>
            ...