0

I'm trying to get html code of a child element using following code :

HTML

<div id="cont">
<img src="http://fakeimg.pl/230x153/555/FFF/?text=1" alt="">
</div>

JQUERY

$(function() {
  console.log($("#cont").find("img").html());
});

I would like to have <img src="http://fakeimg.pl/230x153/555/FFF/?text=1" alt=""> in my console

But Its returning nothing. I dont know whats wrong. I'm pretty sure I'm making some silly mistake.

Thanks.

Link to jsBin : http://jsbin.com/urahos/1/edit


EDIT

Correct Answer

I dont know someone just posted the right answer and removed it.

Following is what I was looking for :

$(function(){
  $("#cont").find("img")[0].outerHTML;
});
Kuldeep Daftary
  • 611
  • 3
  • 16
  • 31
  • *"I dont know whats wrong."* `.html` returns the **inner** HTML of an element. An image element cannot have children, so its inner HTML is an empty string. – Felix Kling Jun 19 '13 at 10:22

4 Answers4

0

This will do it:

$(function() {
  console.log($("#cont").html());
});

You want to read the .html() of the #cont where the img is in.

supersize
  • 13,764
  • 18
  • 74
  • 133
0

console.log($("#cont").html())

ankit singh
  • 367
  • 1
  • 3
  • 13
0

It would be like this:

console.log($("#cont").html())
skparwal
  • 1,086
  • 6
  • 15
-1
$(function() {
  var result=$("#cont").children('img').attr("src");
  console.log(result);
});
karthi
  • 889
  • 12
  • 24