We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Getting a DHTML node as an html string

zumpoof
zumpoof asked
on
Medium Priority
309 Views
Last Modified: 2006-11-18
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>'
Comment
Watch Question

Commented:
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
IT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
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>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.