Get selected element's outer HTML | jquery out html
Noneedtogenerateafunctionforit.Justdoitlikethis:$(a).each(function(){vars=$(this).clone().wrap(
).parent().html();console.log(s);});(Yourbrowsersconsolewillshowwhatislogged,bytheway.Mostofthelatestbrowserssincearound2009havethisfeature.)Themagicisthisontheend:.clone().wrap(
).parent().html();TheclonemeansyourenotactuallydisturbingtheDOM.Runitwithoutitandyoullseeptagsinsertedbefore/afterallhyperlinks(inthisexample),whichisundesirable.So,yes,use.clone().Thewayitworksisthatittak...
No need to generate a function for it. Just do it like this:
$(a).each(function(){ var s = $(this).clone().wrap(<p>).parent().html(); console.log(s); });(Your browsers console will show what is logged, by the way. Most of the latest browsers since around 2009 have this feature.)
The magic is this on the end:
.clone().wrap(<p>).parent().html();The clone means youre not actually disturbing the DOM. Run it without it and youll see p tags inserted before/after all hyperlinks (in this example), which is undesirable. So, yes, use .clone().
The way it works is that it takes each a tag, makes a clone of it in RAM, wraps with p tags, gets the parent of it (meaning the p tag), and then gets the innerHTML property of it.
EDIT: Took advice and changed div tags to p tags because its less typing and works the same.