How to get a HTML element from a string with jQuery

Viewed 99748

I'm looking for a way to get a HTML element from a string that contains HTML. Is it possible to use a jQuery selector to do this?

I have a Javascript function that gets an entire page from the server, but I only need one element from that page.

5 Answers

You can use $.find

$(document).ready(function() {
  var htmlVal = "<div><span class='im-here'>Span Value</span></div>";
  var spanElement = $(htmlVal).find("span");
  var spanVal = spanElement.text();

  alert(spanVal);
});
Related