0

Here is the problematic part of my code, run inside .each(function(){ });

$('img','<div>'+ed.selection.getContent({format: 'html'})+'</div>').each(function(){  
  $img=$('<img/>').attr('src',$(this).attr('src'));
  alert($('<p>'+$img+'</p>').html());
  if ($(this).attr('height').length>0){
    $img.attr('height',$(this).attr('height'));
  }
  if ($(this).attr('width').length>0){
    $img.attr('width',$(this).attr('width'));
  }
  alert($img.html());
});

First, i'm working with the selected tinyMCE content in html format which is fust fine as jQuery recognizes it properly. $img.html() returns empty value, not undefined but just blank. Tested both FF 3.6 and IE8. Can someone explain, please?

Z. Zlatev
  • 4,318
  • 27
  • 34

4 Answers4

2

The html() function only returns the contents of the element. Since <IMG> is technically empty, you get an empty string.

If you had this:

<span>The text</span>

and you asked for $span.html(), you'd get just The text, without the enclosing tags.

Seb
  • 24,063
  • 5
  • 57
  • 82
1

If you are interested in accessing the <img>'s content then have a look at Question number 298049

Community
  • 1
  • 1
ernd enson
  • 1,694
  • 1
  • 14
  • 28
0

.html() gives you the innerHTML of an element i.e. what's inside it. An <img> doesn't have anything inside it - it's an empty element.

You have the right idea by wrapping it and taking the innerHTML of that.

Greg
  • 295,929
  • 52
  • 357
  • 326
0

An image element doesn't have any inner HTML, therefore the method can't return any.

tvanfosson
  • 490,224
  • 93
  • 683
  • 780