Reputation: 43893
In my jquery code, I am using an ajax get function to get the html code of a page. I want to then get a certain element from it, but when I try to do that, jquery gives this error:
SCRIPT5022: Syntax error, unrecognized expression:
on the html string.
$.ajax({
url: myURL,
dataType: 'html',
success: function (data) {
g = $('#MainLeftCell', $(data)).html(); // ERROR occurs here
alert(g);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("An error occured when searching.");
}
});
I think it because there might be some weird sequence of code in the html string that the jquery parser cannot recognize. The page its getting it from is a SharePoint page.
Is there someway to fix this?
Upvotes: 2
Views: 126
Reputation: 95056
Most likely your HTML isn't valid for usage with the $()
method. Pass it to $.parseHTML
first.
var g = $('#MainLeftCell', $.parseHTML(data)).html();
If that gets rid of the error but still doesn't work, most likely #MainLeftCell
is a top level element and you'll have to use .filter to get it.
var g = $($.parseHTML(data)).filter('#MainLeftCell').html();
A more general solution is
var g = $("<div>").html(data).find("#MainLeftCell").html();
Upvotes: 3
Reputation: 36541
you have a syntax error there...why don't you use find()
..its cleaner
g = $(data).find('#MainLeftCell').html();
Upvotes: 0