Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
152
rated 0 times [  154] [ 2]  / answers: 1 / hits: 17890  / 11 Years ago, thu, october 24, 2013, 12:00:00

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);
});

More From » jquery

 Answers
27

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() );

[#74773] Tuesday, October 22, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brookelynshaem

Total Points: 468
Total Questions: 98
Total Answers: 101

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;