Zaje
Zaje

Reputation: 2309

Javascript getting DOM children as a String

I have an HTML document with the following code

    <html>
<head></head>
<body>
<div id="wrapper">
<div class="label">First Name : </div>
<div class="value">John Doe</div>
</div>
</body>
</html>

Using javascript I want to access the contents of div#wrapper as a string like

var s = '<div class="label">First Name : </div><div class="value">John Doe</div>';

tried

var wrapperContent = document.getElementBydId("wrapper").innerHTML; // returns First Name : John Doe

but this gives me only the text values. Anyways I can achieve this ??

Upvotes: 0

Views: 209

Answers (4)

mway
mway

Reputation: 4392

innerHTML should work fine. For example, try document.getElementBydId("wrapper").innerHTML on this page.

Be sure you're executing the Javascript at either the end of the document, or within some incarnation of the window.onload event.

Upvotes: 1

Nican
Nican

Reputation: 7935

What you posted should work.

I believe your problem is that you are trying to get the innerHTML of an element that does not have a parent yet.

Upvotes: 1

joni
joni

Reputation: 5462

It works if you type document.getElementById instead of document.getElementBydId =)

You just mistyped..

EDIT: Look here, it works! http://www.jsfiddle.net/rVa9t/

Upvotes: 0

kobe
kobe

Reputation: 15835

use jquery's method

$('#wrapper').html 

Upvotes: 0

Related Questions