9

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");
    }); 
ABHILASH SB
  • 2,122
  • 2
  • 21
  • 31

2 Answers2

16

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() );
MrCode
  • 63,975
  • 10
  • 90
  • 112
1

How about:

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

ST3
  • 8,826
  • 3
  • 68
  • 92