#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: | ||
Blocked by: | Blocking: |
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 (4)
comment:1 Changed 11 years ago by
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 follow-up: 3 Changed 11 years ago by
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 Changed 11 years ago by
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/>');
comment:4 Changed 11 years ago by
You're appending stuff (that happens to begin with a text node) to an existing DOM element. The text node is not in the jQuery collection.
But that's not important right now, this isn't a bug. Ask for help on the forum or StackOverflow if you need it.
[] is the syntax for attribute selectors