Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#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 8 years ago by Rick Waldron

Resolution: invalid
Status: newclosed

[] is the syntax for attribute selectors

comment:2 Changed 8 years 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 8 years 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/>');

comment:4 Changed 8 years ago by dmethvin

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.

Note: See TracTickets for help on using tickets.