3

My question is quite simple.. I need to convert an Element object into an html string

var thumb = new Element('img',{'src':"big.jpg"});
console.log( thumb.?????() ); //some magical method here

should return (as a string)

'<img src="_big.jpg">'

I've tried the .get('html') but it returns nothing, obviously because the img tag has nothing inside.

Thanks in advance

pleasedontbelong
  • 19,542
  • 12
  • 53
  • 77

3 Answers3

3

Just proxy it.

var html = new Element('div').adopt(yourel).get('html');
Dimitar Christoff
  • 26,147
  • 8
  • 50
  • 69
1

Have you tried outerHTML? I am pretty sure all you need is:

var html = selector.outerHTML;
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
  • (+1 I've learned something today) apparently it's not compatible with all navigators http://stackoverflow.com/questions/1700870/how-do-i-do-outerhtml-in-firefox, and it might need some hacks. I wanted to know if there was some function on the core of mootools that could do that for me n_n – pleasedontbelong Jun 22 '12 at 15:36
  • Actually it has been added, check out some of the answers to [get-selected-elements-outer-html](http://stackoverflow.com/questions/2419749/get-selected-elements-outer-html). It appears that most browsers started supporting it in 2011. – Josh Mein Jun 22 '12 at 15:42
  • that's jquery :P i'm using mootools – pleasedontbelong Jun 22 '12 at 15:43
  • It is using jquery to get the element. `outerHTML` is just plain old javascript. – Josh Mein Jun 22 '12 at 15:44
  • nah =P.. its not default javascript.. check this http://jsfiddle.net/pleasedontbelong/arjCC/ it works fine on Chrome and IE, but not on FF – pleasedontbelong Jun 22 '12 at 15:50
  • `outerHTML` is a javascript option that was initially supported only by IE. It gained support from Chrome and Firefix in 2011. As for Firefox it started being supported specifically in [Firefox 11](https://developer.mozilla.org/en/Firefox_11_for_developers). – Josh Mein Jun 22 '12 at 15:59
  • Also to note your jsFiddle worked for me in Firefox and Chome! – Josh Mein Jun 22 '12 at 16:00
  • ahh that's it :P I'm using and older version of FF. – pleasedontbelong Jun 22 '12 at 16:13
0

Returns a collection of elements from a string of html.

http://mootools.net/docs/more/Element/Elements.From

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63