String from Javascript DOM object

T

Tim

Need help! I am a little stuck. I am trying to get the string
representation of a object that I created in memory.

var fieldImage = document.createElement("img");
fieldImage.src = domObj.fieldImage;

I am trying to get the formatted string for: fieldImage like "<img
src="...."/>" but don't see a method to do that.

Ideas? Help!
 
M

Martin Honnen

Tim said:
Need help! I am a little stuck. I am trying to get the string
representation of a object that I created in memory.

var fieldImage = document.createElement("img");
fieldImage.src = domObj.fieldImage;

I am trying to get the formatted string for: fieldImage like "<img
src="...."/>" but don't see a method to do that.

fieldImage.outerHTML
is what IE 4 introduced and by now most browsers beside Mozilla browsers
offer.
 
T

Thomas 'PointedEars' Lahn

Martin said:
fieldImage.outerHTML
is what IE 4 introduced and by now most browsers beside Mozilla browsers
offer.

outerHTML will not yield the true source code, though. It will also not
work as expected with XHTML, which is what the OP might want (if the `/' in
the start tag is any indication). Fortunately, XML serializers are built-in
at least in Gecko:

var s = new XMLSerializer().serializeToString(
nodeRef, "application/xhtml+xml");

It is rather unclear why the OP would want the source code when he has the
object; however, if the built-in serializers are not satisfactory, it is
rather easy to create one (merely an exercise in string concatenation) as
the properties that need to be accessed are known (especially here as the OP
appears to assign them himself) and can be feature-tested. Only
transforming event-handler attributes back to string might prove a bit tricky.


PointedEars
 
J

Jorge

Tim said:
Need help! I am a little stuck. I am trying to get the string
representation of a object that I created in memory.

var fieldImage = document.createElement("img");
fieldImage.src = domObj.fieldImage;

I am trying to get the formatted string for: fieldImage like "<img
src="...."/>" but don't see a method to do that.

Ideas? Help!

alert(fieldImage.outerHTML);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,098
Messages
2,570,625
Members
47,236
Latest member
EverestNero

Latest Threads

Top