0

How its result 2? can anyone explain

What is the output of this code? <div><p>1</p></div> <div>2</div> <script> alert($("p").parent().siblings().eq(0).text()); </script>

4th Jan 2017, 12:05 PM
Kabir Mandal
Kabir Mandal - avatar
2 Answers
+ 1
You start with the "p" tag, its parent tag (.parent()) is the "div", now you want all siblings of this "div" tag (.siblings()). The method .eq(0) specifies that you want the first sibling of the "div" tag which contains the paragraph, this is the div with the text of "2". For example: Note that i gave the divs an id. <div id="div1"> <p>1</p> </div> <div id="div2"> 2 </div> alert($("p").parent().attr('id')); // output div1 alert($("p").parent().siblings().attr('id')); // output div2 alert($("p").parent().siblings().eq(0).attr('id')); // output div2 alert($("p").parent().siblings().eq(0).text()); // output 2
4th Jan 2017, 1:13 PM
Niklas Vogel
+ 1
b our 2
28th Oct 2022, 7:57 AM
sajad heydari
sajad heydari - avatar