Bug Tracker

Modify

Ticket #2631 (closed bug: invalid)

Opened 6 years ago

Last modified 6 years ago

Something wrong with :last-child

Reported by: chrishandorf Owned by:
Priority: major Milestone: 1.2.4
Component: core Version: 1.2.3
Keywords: Cc:
Blocking: Blocked by:

Description

Something must be wrong with the jQuery ":last-child" selector. In my program this expression returns an empty object:

var newObject = $("#rootFolder:last-child");

But if I simply change the code to this it works fine:

var newObject = $("#rootFolder").children(":last-child");

Change History

comment:1 Changed 6 years ago by davidserduke

  • Status changed from new to closed
  • Resolution set to worksforme

If I understand it all correctly, those 2 you listed are the same.

$("#rootFolder").children(":last-child");

looks at all the children of element with id="rootFolder" and returns the last one

$("#rootFolder:last-child");

gets the element with id="rootFolder" and filters it to see if it is the last child. So

<body>
  <div id="rootFolder"></div>
  <div></div>
</body>

will return no items for that. It is equivilent to

$("#rootFolder").filter(":last-child");

instead.

If I'm misunderstanding, then feel free to reopen the ticket with an actual test case (including html markup) which shows the problem.

comment:2 Changed 6 years ago by chrishandorf

  • Status changed from closed to reopened
  • Resolution worksforme deleted

OK - here is the HTML that demonstrates the problem. It will output "test1=0 test2=1" when it seems like they should both be set to "1":

<html>
<body>
   <div id="rootFolder">
      <div class="test"></div>
   </div>
   <br>
   <div class="log">
   </div>
   
<script src="jquery-1.2.3.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
   var test1 = $("#rootFolder:last-child");
   var test2 = $("#rootFolder").children(":last-child");
   $(".log").append("test1="+test1.length+" test2="+test2.length+"<br>");
});
</script>
</body>
</html>

comment:3 Changed 6 years ago by scott.gonzal

  • Status changed from reopened to closed
  • Resolution set to invalid

You are using the selector incorrectly. As David has pointed out, "#rootFolder:last-child" will first find #rootFolder, then filter it based on whether or not is is the last child of its parent. What you want is "#rootFolder > :last-child" Which will find #rootFolder, then all its children, then filter the children down to the last child (equivalent to test2).

Perhaps it will help you to think about the context in which all the ":" selectors are applied. For example, ":parent" will filter the current elements based on whether or not they are parents; it won't find the parents of the current elements.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.