Skip to content

Instantly share code, notes, and snippets.

@kapusta
Created September 30, 2011 19:37
Show Gist options
  • Save kapusta/1254761 to your computer and use it in GitHub Desktop.
Save kapusta/1254761 to your computer and use it in GitHub Desktop.
jQuery addressable AJAX/HTML fragments
$.ajax({
url: "/some_path/foo.php", /* a url that returns a fragment of html */
dataType: "html", /* ask for and expect html */
success: function(html){
var $fragment = $("<div/>") /* make a fragment (createDocumentFragment() won't work) */
.html(html) /* put the html into the div so we can get at stuff in jQuery */
.find("#some_id"); /* we can now .find() things! */
/* if there's a <span id="some_id" data-foo="bar"></span> in out returned html, then... */
console.log($fragment.data("foo")); /* ...logs "bar" */
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment