I need to show the code of some Divs in page. Div should be cloned into pre tag with proper spacing and identation and should work in ie8.
I'm a starter so don't really know if i'm doing it correct way, so far i wrote this
Html
//create button after div
$("<div class='btn'>click to show code</div>").insertAfter(".content-wrapper");
//create pre wrapper after button
$("<pre></pre>").insertAfter(".btn");
//hide the pre so can slidetoggle later
$("pre").hide();
$(".btn").one("click", function() {
var cloned = $(this).prev().clone();
var code = $(cloned).html().replace(/</g, "<").replace(/>/g, ">");
$(this).next().append(code);
});
$(".btn").click(function() {
$(this).next().slideToggle("fast", function() {
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="wrapper-all">
<div class="content-wrapper">
<div class="example-div">
<span>lorem ipsum first</span>
</div>
</div>
</div>
<div class="wrapper-all">
<div class="content-wrapper">
<div class="example-div">
<span>lorem ipsum second</span>
</div>
</div>
</div>
It dynamically adds, button and pre after the .content-wrapper divs which can be slide toggled.
ie8 completeley ignores whitespaces, and in chrome,firefox some unwanted whitespaces appearing, the outputed code should respect the identation of the original divs, but remove the spaces from left and code should start from zero whitespace to the left.