Skip to main content

Bug Tracker

Side navigation

#11407 closed bug (invalid)

Opened February 28, 2012 09:02AM UTC

Closed February 28, 2012 01:10PM UTC

Last modified March 02, 2012 01:18PM UTC

[ ] 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/

Attachments (0)
Change History (4)

Changed February 28, 2012 01:10PM UTC by rwaldron comment:1

resolution: → invalid
status: newclosed

[] is the syntax for attribute selectors

Changed February 28, 2012 01:14PM UTC by dmethvin comment:2

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.

Changed March 02, 2012 08:09AM UTC by anonymous comment:3

Replying to [comment:2 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/>');

Changed March 02, 2012 01:18PM UTC by dmethvin comment:4

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.