0

Let say that my html is :

<div class="newEntry">
    <h1 class="entry"><img src=""></h1>
     <p>Josh : 2006</p>
</div>

Just for debugging purpose when i wrote

$("#newEntry").find("h1").find("img");

it is showing something like :

[prevObject: r.fn.init(0)]

while i want to see output as html items something like

 <h1 class="entry"><img src=""></h1>

Statements from Console

document.getElementsByClassName("newEntry");
[]
$("newEntry>h1")
[prevObject: r.fn.init(1)]
$("#newEntry").find("h1").find("img");
[prevObject: r.fn.init(0)]
$("#newEntry").find("h1");
[prevObject: r.fn.init]length: 0prevObject: r.fn.init {}__proto__: Object(0)
$("#newEntry").find("h1").find("img");
[prevObject: r.fn.init(0)]
$("#newEntry").find("h1").find("img").html();
undefined
$("#newEntry").find("h1").html();
undefined
$("#newEntry").html();
undefined
dinesh kandpal
  • 738
  • 7
  • 16
  • What exactly are you logging? Please include your `console.log` statement. – 4castle Aug 11 '17 at 06:58
  • `$(".newEntry").find("h1").find("img").parent()[0].outerHTML` – Jaromanda X Aug 11 '17 at 07:00
  • If you want to show the HTML of whatever you're looking for, you need to ask for the HTML. By default, the [jQuery find](https://api.jquery.com/find/) function returns an object. – rkeet Aug 11 '17 at 07:33

2 Answers2

2

You have used #newEntry which is used to get the elements by ID in jquery. But in your <div> you haven't defined a ID. So, you need to use a class selector.

You need to use the .html() to get the exact content.

$(".newEntry").find("h1").html();
Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62
0

[prevObject: r.fn.init(0)] that the element your are looking for was not found. Because your are using '#' symbol for a class. You should do :

<div class="newEntry">
    <h1 class="entry"><img src=""></h1>
     <p>Josh : 2006</p>
</div>

and

$(".newEntry:first").find("h1").find("img");

or

<div id="newEntry">
    <h1 class="entry"><img src=""></h1>
     <p>Josh : 2006</p>
</div>

and

$("#newEntry").find("h1").find("img");
  • $(".newEntry") is again showing map kind of structure !!! do i need to make any setting changes in chrome console !! [prevObject: r.fn.init(1)] – dinesh kandpal Aug 11 '17 at 07:04
  • Look at this [example](https://jsfiddle.net/msrx037L/) I used the attr to show that i can get the image. And no you should not need to change any settiings from your chrome comsole to have the good result – Alexandre Thyvador Aug 11 '17 at 07:30
  • `[prevObject: r.fn.init(1)] `, notice `(1)`, it found the object right? Try what Jaromanda suggested in the question comments? `$(".newEntry").find("h1").find("img").parent()[0].outerHTML` – rkeet Aug 11 '17 at 07:32
  • What is the point of getting an element then its parent to finally get the first child ? Cann't you simply do : `$(".newEntry").find("h1")[0].outerHTML` ? – Alexandre Thyvador Aug 11 '17 at 07:36
  • `

    some title

    title with image

    ` The difference being that your code returns both the `

    ` tags, the one originally suggested by Jaromanda returns only the one with an image. Though the `find()` could be simplified to: `.find('h1 > img')`

    – rkeet Aug 11 '17 at 09:29
  • That's what i meant. This idea was not wrong but too complicated. You can do even simpler, just set an id on your img and get it through the id. – Alexandre Thyvador Aug 11 '17 at 09:33