Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting the next sibling of the item just clicked

I have a ul which contains images. These images are profile images fetched from twitter and appended into the ul dynamically. Once the user clicks on any of the images, I need to also cache the node of the image right next to it (the next sibling). I tried using the next() selector like below, but what gets logged in the console is a message I do not understand. Here is the code:

$("ul#container").on("click", "img", function(){
  var nextImage = $(this).next();
  console.log(nextImage);
}

Here is what gets logged in the console:

[prevObject: p.fn.p.init[1], context: , selector: ".next ()"]

Could you please help me understand what I am doing wrong? Thanks for reading!

like image 497
Andreas Avatar asked Dec 20 '12 14:12

Andreas


People also ask

What is next sibling in html?

nextSibling returns the next node (an element node, a text node or a comment node). Whitespace between elements are also text nodes. nextElementSibling returns the next element (not text and comment nodes).

How to get next sibling element in jQuery?

jQuery next() Method The next() method returns the next sibling element of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse forward along the next sibling of DOM elements.


2 Answers

Nothing wrong.

That is how Chrome now logs jQuery objects.

Now go have some fun with it!

like image 154
Naftali Avatar answered Oct 01 '22 00:10

Naftali


Try:

$("ul#container").on("click", "img", function(){
  var nextImage = $(this).next().get(0);
  console.log(nextImage);
}

Or

Try This Plugin For Chrome

like image 32
Akhil Sekharan Avatar answered Oct 01 '22 02:10

Akhil Sekharan