Get the text inside <h2> element from an ajax jquery post response

16,369

Solution 1

You can pass HTML to jQuery and use it in the same way as if the element was on the DOM, for example with find():

console.log( $(resultData).find('h2').text() );

If your HTML doesn't have a root element then you can wrap it like so:

resultData = '<div>' + resultData + '</div>';
console.log( $(resultData).find('h2').text() );

Solution 2

How about:

$(resultData).find('h2').text()

Share:
16,369
ABHILASH SB
Author by

ABHILASH SB

Updated on June 15, 2022

Comments

  • ABHILASH SB
    ABHILASH SB almost 2 years

    Is there any way to get the text inside an element which is a response from an ajax jquery load. I need to get the text inside element which is present inside the response text from ajax page. Following is my ajax code:

        var url = '...';
        var saveData = $.ajax({
            type: 'POST',
            url: url,
            data: {data : data},
            dataType: "text",
            success: function (resultData) {
                    callback(resultData); // need to get the <h2> text here..
            }
        });
        saveData.error(function () {
            console.log("Request to API not send");
        });