Pompair
Pompair

Reputation: 7311

Getting inner html with jQuery?

I have a javascript/jQuery block as a callback after $.get function:

function myCallBack(data, textStatus) {
  var text1 = $(data).html();
  document.write(text1);
} 

The data contains html data ok. I'd like to strip the html and get only inner html into text1 variable. For some reason it doesn't work. Firebug kinda "crashes" upon executing line 'var text1 = ...'

Edited:

My data variable contains:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3.org/TR/xhtml1/…;\r\n\r\n<html xmlns="w3.org/1999/xhtml">;\r\n
<head>\r\n 
<title></title>\r\n
</head>\r\n
<body>\r\n Testing...\r\n</body>\r\n
</html>\r\n 

And I'd like to parse the part between body tags.

Upvotes: 0

Views: 7642

Answers (3)

Andreas Grech
Andreas Grech

Reputation: 107940

You mean you want the inner text?

var text1 = $(data).text();

[Update]

Try it with this regular expression:

var bodyText = new RegExp(/<body[^>]*>([\S\s]*?)<\/body>/).exec(data)[1];

Upvotes: 6

Mathias F
Mathias F

Reputation: 15891

You perform a webrequest by get. That implies that the result will only be a string.

var text1 = data ;

Is all you can get. There is no DOM-object you can traverse. You only get this if you access elements on you own page.

Upvotes: 0

Gumbo
Gumbo

Reputation: 655129

Try this:

$(data)[1].data

But I think that just works with a specific example and not in general.

Upvotes: 0

Related Questions