Ticket #11407 (closed bug: invalid)
[ ] square brackets wierd behavior in appendTo()
| Reported by: | Madevil | Owned by: | |
|---|---|---|---|
| Priority: | undecided | Milestone: | None |
| Component: | unfiled | Version: | 1.7.1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
var $index = 0, $title = 'boo';
$('[' + $index + ']<span class="item">' + $title + '</span><br/>').appendTo('#main');
should expect to see [0]boo as result, but only come out with boo instead
but it works fine with .append()
$('#main').append('[' + $index + ']<span class="item">' + $title + '</span><br/>');
or with tags wrapped up
$('<div>[' + $index + ']<span class="item">' + $title + '</span></div>').appendTo('#main');
a working example http://jsfiddle.net/VKQgu/
Change History
comment:1 Changed 16 months ago by rwaldron
- Status changed from new to closed
- Resolution set to invalid
comment:2 follow-up: ↓ 3 Changed 16 months ago by dmethvin
The problem there is that the leading [0] is a text node outside any DOM element, and is stripped by $(); this is correct behavior since text nodes do not generally live in a jQuery collection. Wrap the whole thing in a div and it reappears.
comment:3 in reply to: ↑ 2 Changed 16 months ago by anonymous
Replying to dmethvin:
The problem there is that the leading [0] is a text node outside any DOM element, and is stripped by $(); this is correct behavior since text nodes do not generally live in a jQuery collection. Wrap the whole thing in a div and it reappears.
uh... why is this work as expect then??
$('#main').append('[' + $index + ']<span class="item">' + $title + '</span><br/>');
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

[] is the syntax for attribute selectors