Side navigation
#2631 closed bug (invalid)
Opened April 01, 2008 04:10PM UTC
Closed April 07, 2008 06:48PM UTC
Last modified October 14, 2008 10:31AM UTC
Something wrong with :last-child
| Reported by: | chrishandorf | Owned by: | |
|---|---|---|---|
| Priority: | major | Milestone: | 1.2.4 |
| Component: | core | Version: | 1.2.3 |
| Keywords: | Cc: | ||
| Blocked by: | Blocking: |
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");
Attachments (0)
Change History (3)
Changed April 02, 2008 04:22AM UTC by comment:1
| resolution: | → worksforme |
|---|---|
| status: | new → closed |
Changed April 07, 2008 04:16PM UTC by comment:2
| resolution: | worksforme |
|---|---|
| status: | closed → reopened |
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>
Changed April 07, 2008 06:48PM UTC by comment:3
| resolution: | → invalid |
|---|---|
| status: | reopened → closed |
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.
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
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.