Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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");
    }); 
like image 721
ABHILASH SB Avatar asked Oct 24 '13 06:10

ABHILASH SB


2 Answers

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() );
like image 52
MrCode Avatar answered Nov 08 '22 23:11

MrCode


How about:

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

like image 37
ST3 Avatar answered Nov 09 '22 00:11

ST3