0

I am trying to do a simple thing maybe i am missing something:

var span = $(document.createElement("span")); // Create a span
var img = new Image(); // Create an image and set source
img.src = config.graph_url + member.facebook_id + "/picture?width=24&height=24";
span.append($(img)); // Add the image inside the span

How can i get the html <span><img src="_SOURCE_"/></span>? span.html() outputs <img src="_SOURCE_"/>.

Eldar
  • 591
  • 3
  • 16
  • Correct. That's how [`.innerHTML`](https://developer.mozilla.org/en-US/docs/DOM/element.innerHTML)/[`.html()`](http://api.jquery.com/html/#html1) works. – Matt Ball Apr 08 '13 at 22:20
  • 1
    See http://stackoverflow.com/a/2419877/174469 for the answer on how to get the outerhtml – Gordon Tucker Apr 08 '13 at 22:22

2 Answers2

1

It's working fine. The html() method sets or returns the content (innerHTML) of the selected elements

In an HTML document, .html() can be used to get the contents of any element. If the selector expression matches more than one element, only the first match will have its HTML content returned.

So you can use .outerHTML for this

$('span')[0].outerHTML;
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Sachin
  • 40,216
  • 7
  • 90
  • 102
0

If you want this <span><img src="_SOURCE_"/></span>

Try something like this

$('span')[0].outerHTML;
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111