1

I found this wonderful script on StackOverflow, which returns the code for any DOM element with all styles intact: https://stackoverflow.com/a/6310120/3001859

The only problem with it is, (it looks like) it doesn't compute and return the styles for the original element. It only does it for the inner elements. How can I modify this - so it returns the complete code, with the styles for the original element too?

(I cannot comment on the original thread coz of my low rep)

Community
  • 1
  • 1
user3001859
  • 265
  • 1
  • 3
  • 13

1 Answers1

0

The problem with the recursive call is that it has to have and start and end case. Without a lot of work you can accomplish this by making a parent of the selected element and using that.

/*grab the page*/
var orgin = $('body').html();
/*grab the contents you are looking for, then replace the body*/
$('body').html($('widgets'));
/*get the widgets style*/
var htm = $('body')[0].serializeWithStyles();
/*give the body back to origin*/
$('body').html(orgin);
/*append the results*/
$('prepend').html($("<div>").text(htm).html())

http://jsfiddle.net/1vv8nx1u/2/

You can strip the body tags out if you don't need those, that is a simple parse.

Roger
  • 3,226
  • 1
  • 22
  • 38