What I am trying to do here is to modify child elements and add code before and after the matched elements, then return full HTML string to update a template file.
So far this has been successful but one small problem which I can not seem to solve. for example:
var htmlObj = $('<div/>').html(html).contents();
$htmlDom = htmlObj.parent().find('*');
$htmlDom.each(function(i, el) {
if(typeof($(el).data('key')) !== 'undefined') {
$(el).before(codeBefore);
$(el).after(codeAfter);
}
});
html = $htmlDom.html();
Now this works fine to add the code before and after. But the problem is if the markup is different for example: everything is wrapped around
This 1st line of html with is ignored unless I change to $htmlDom.parent().html();
And if there is for example additional wrap then to get this line to work I need to change to $htmlDom.parent().parent().html();
The problem is the markup will be dynamic so we don't know if the code will be wrapped or which HTML tags are used. So what I need here is a way to modify the html output to string the full markup.