views:

838

answers:

3

I've created an element as shown:

  var imageElement =  new Element('img', {
                    'src': item.Url,
                    'alt': item.Id,
                    'height': height + 'px',
                    'width': width + 'px', 
                    'styles': {
                        'padding-left': paddingLeft + 'px',
                        'padding-top': paddingTop + 'px'
                    }
                });

When I put a break point in to bring up a debugger and see what imageElement.get('html') returns, it's an empty string.

Why might this be? Isn't that how you're suppose to get the innerHTML?

EDIT: Oh, how do I get its HTML then?

+1  A: 

Image elements are singleton tags; they don't have inner HTML.

Randolpho
Oh, how do I get its HTML then?
JamesBrownIsDead
The better question is why do you need it? You've already created the image element. Why not just add it to DOM and be done with it? If you haven't found that portion, you'll need to use one of the inject functions Oskar hinted at. http://mootools.net/docs/core/Element/Element#Element:inject
Randolpho
+1  A: 

innerHTML is, as the name implies, the HTML inside the element, ie. its contents. There is no content in an <img> as it is an empty element by definition.

In IE you also get outerHTML. No good cross-browser, but can be useful for debugging. Otherwise you would be limited to eg. appending the image into an empty div and getting the innerHTML of the div.

                'height': height + 'px',
                'width': width + 'px', 

Unlike CSS there is no unit for HTML width/height, these properties are plain integers.

bobince
+2  A: 

I'm unsure why would you want the IMG's HTML when you can access it via its Object, but here you are:

HTML:

<div id="image-wrap"></div>

MooTools:

var imageElement =  new Element('img', {
   'src': item.Url,
   'alt': item.Id,
   'height': height,
   'width': width, 
   'styles': {
       'padding-left': paddingLeft + 'px',
       'padding-top': paddingTop + 'px'
   }
}).inject($('image-wrap'));

console.log(imageElement.getParent().get('html'));

You can also create the wrapping element on-the-fly and then fetch its contents.

Oskar Krawczyk