jQuery Traversing | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

jQuery Traversing

Why is the following alert 2 and not 1 ?? <div><p>1</p></div> <div>2</div> <script> alert($("p").parent().siblings().eq(0).text()); </script>

31st Jul 2017, 11:17 AM
Cristi Musat
Cristi Musat - avatar
2 Answers
+ 13
When you use siblings() it selects all of them on that level but doesn't includes the one you're initially selecting. So in this case, you selected the <p> parent which is the first div listed. Now the only sibling is the second div with the number 2. So that would be at position 0.
31st Jul 2017, 11:23 AM
Dev
Dev - avatar
0
thank you sir ! clean and to the point
1st Aug 2017, 9:39 AM
Cristi Musat
Cristi Musat - avatar