• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Getting a DHTML node as an html string

I've created a 'div' using DHTML. Is it possible to see what it looks like as a string of HTML. So something like this:

var d=document.createElement('DIV');
d.style.border='1px solid';
d.innerHTML='Hi there';

Converted into:

'<DIV STYLE="border:1px solid">Hi there</DIV>'
0
zumpoof
Asked:
zumpoof
1 Solution
 
jplevyakCommented:
There is an outerHTML property that would return the full HTML as a string but this is only supported in IE. However, there was already a post on this site with this issue where some experts have provided workarounds.

How to get outerHTML in IE and Mozilla (Netscape)
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21221807.html
0
 
Michel PlungjanIT ExpertCommented:
Since I cannot add to that question, here is a combination of yours and theirs with your dynamic generation used for the hidden div:


<body>
<div id="theDiv" style="border:solid 1px red;width:300">
<p>abc</p>
<p>def </p>
</div>

<form id="abc" name="myForm" action="xyx.com" target="_blank">
  <p><input type="text" name="t2"> <input> <input type="submit"> </p>
</form>

<script>
var d=document.createElement('DIV');
d.id="x";
d.style.border='1px solid';
d.innerHTML='Hi there';
document.body.appendChild(d)
</script>


<script>
function getOuterHTML(obj){
 
 var temp=document.getElementById(obj).cloneNode(true)
 var d=document.createElement('DIV');
 d.style.display='none';
 d.appendChild(temp)
 var outer=d.innerHTML
 d.innerHTML=""
 return outer
}
alert(getOuterHTML("theDiv"))
alert(getOuterHTML("abc"))
alert(getOuterHTML("x"))
</script>

</body>
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now