ajax - Extract part of HTML document in jQuery -


i want make ajax call html-returning page, extract part of html (using jquery selectors), , use part in jquery-based javascript.

the ajax retrieval pretty simple. gives me entire html document in "data" parameter of callback function.

what don't understand how handle data in useful way. i'd wrap in new jquery object , use selector (via find() believe) part want. once have i'll passing off javascript object insertion document. (this delegation why i'm not using jquery.load() in first place).

the get() examples see seem variations on this:

$('.result').html(data); 

...which, if understand correctly, inserts entire returned document selected element. not suspicious (doesn't insert <head> etc?) it's coarse want.

suggestions on alternate ways welcome.

you can use standard selector syntax, , pass in data context selector. second parameter, data in case, our context.

$.post("getstuff.php", function(data){   var maindiv = $("#maindiv", data); // finds <div id='maindiv'>...</div> }, "html"); 

this equivalent doing:

$(data).find("#maindiv"); 

depending on how you're planning on using this, $.load() may better route take, allows both url , selector filter resulting data, passed directly element method called on:

$("#mylocaldiv").load("getstuff.php #maindiv"); 

this load contents of <div id='maindiv'>...</div> in getstuff.php our local page element <div id='mylocaldiv'>...</div>.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -