0

I have a DOMelement like

text

and I have saved this in a variable elem in a function

function(elem){ var elem=elem; }

And NOW I want to save this dom object as a string like

<div class="testdiv"><div class="delete">text</div>

How would you manage this? I tried sth. like elem.toString() which didn't work or elem.html() which just returns the INNER html.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
ho.s
  • 751
  • 3
  • 17
  • 42

2 Answers2

1

Sounds like you want the outer html:

elem.outerHTML
Joe
  • 80,724
  • 18
  • 127
  • 145
  • thx, but..hm..as for the reason I get the element by `var elem= anotherelem.parent('.classname')` the outerHTML property is not defined for this? is there a jquery method for outerHTML? – ho.s Dec 22 '11 at 16:20
  • hm, when I click on the link, the alert says "undefined" – ho.s Dec 22 '11 at 16:32
  • seems as if it just don't work for firefox, but I need to support ff. bummer! – ho.s Dec 22 '11 at 16:38
  • 1
    Hey @ho.s check out this answer for a much more complete solution http://stackoverflow.com/a/3819589/724626 – Joe Dec 22 '11 at 17:40
0

For jQuery you might want to refer it as

var elem = $('#elem');
var strElem = elem[0].outerHTML;
if(strElem == null) {
    strElem = $('<div>').append(elem.clone()).html();
}

http://jsfiddle.net/KRgkc/3/

I.G. Pascual
  • 5,818
  • 5
  • 42
  • 58
  • oh my..ok this works as well as the example above, BUT NOT in Firefox 8 as it seems! it works for chrome, safari..but not for ff! how can this be.... :-( – ho.s Dec 22 '11 at 16:37
  • @ho.s the FF approach is to clone `elem`, append it to a created from scratch `div`, and return the html of that div ;). Otherwise, `outerHTML` – I.G. Pascual Dec 22 '11 at 16:50