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

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
  • 344,413
  • 96
  • 627
  • 693
Sachin
  • 39,043
  • 7
  • 86
  • 102
0

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

Try something like this

$('span')[0].outerHTML;
Matt Ball
  • 344,413
  • 96
  • 627
  • 693
Mohammad Adil
  • 44,013
  • 17
  • 87
  • 109