Skip to content Skip to sidebar Skip to footer

Why Can't I Parse A Ajax Html Get Response In Jquery?

I'm tempering with a Chrome Extension where I use an Ajax-request to get HTML from a requested URL. This works, but I want to get all the text values some certain elements. By exa

Solution 1:

If you are using jquery 1.9, do:

...
success: function(data) {
   var html = $.parseHTML(data); 
   console.log($(html).find( '.heading_bold' ).text()); 
}
..

Because as per jQuery 1.9:: HTML strings passed to jQuery() that start with something other than a less-than character will be interpreted as a selector. Since the string usually cannot be interpreted as a selector, the most likely result will be an "invalid selector syntax" error thrown by the Sizzle selector engine. Use jQuery.parseHTML() to parse arbitrary HTML.

Solution 2:

Maybe you could try to use "load" instead of "$.get()" if you want to insert a portion of the remote document into DOM.

$("#result").load("page.html .heading_bold",function(response){
console.log($(this).find(".heading_bold").val());           
});

Hope this is helpful to you.

Post a Comment for "Why Can't I Parse A Ajax Html Get Response In Jquery?"