5

Possible Duplicate:
jQuery, get html of a whole element

lets say I have:

<span>my span</span>

I would like have html as a string of this span

I use:

var mySpan = $('span');

what to do with the mySpan var to have as a result string "<span>my span</span>"

thanks for any help

Community
  • 1
  • 1
gruber
  • 28,739
  • 35
  • 124
  • 216

3 Answers3

10

I think this will help: http://jsfiddle.net/HxU7B/2/.


UPDATE.

mySpan[0].outerHTML will take a previoulsy selected node and get a native outerHTML property. Since old Firefox versions doesn't have that property, we can use a bit of hack to get html - simply clone node to dummy div and then get this div's innerHTML: $('<div/>').append(mySpan.clone()).html()

Igor Shastin
  • 2,878
  • 2
  • 25
  • 38
2

jQuery can't do that, but regular JavaScript DOM objects can:

var mySpanString = $('span').get(0).outerHTML;
Joe Coder
  • 4,498
  • 31
  • 41
0

As others have stated, you could use outerHTML or clone -> append -> html()

But here is another way to do it,

var nodeName = mySpan[0].nodeName.toLowerCase();
var outerHtml = "<"+nodeName +">"+mySpan.html()+"</"+nodeName +">";
prashanth
  • 2,059
  • 12
  • 13