JQuery method to get one node - instead of find ();

I searched for this for an hour and cannot find the answer.

I use $(xml).find('title')- but it seems to return all the "headers" in all nodes. How to simply get the title in the root directory of a node?

<response stat="OK">
    <title>Some Document</title>
    <menu>
        <item>
            <title>Some Title</title>
            <url>/</url>
        </item>
        <item>
            <title>Some Title 1</title>
            <url>/asdfasdf/</url>
        </item>
    </menu>
</response>

returns "Some DocumentSome TitleSome Title 1"

// Loads the page content and inserts it into the content area
$.ajax({
    dataType:   'xml',
    url:        'someurl',
    success:    function(data, textStatus, XMLHttpRequest) {

        // returns 
        console.log($(data).find('title').text());


    }

I just want the first title! I would prefer not to use xpath, I would prefer to use the cheapest solution.

+3
source share
1 answer

You can use .children(), for example:

console.log($(data).children('title').text());

.children() looking only at the first level down.

+2
source

Source: https://habr.com/ru/post/1763127/


All Articles