Bug Tracker

Modify

Ticket #11407 (closed bug: invalid)

Opened 16 months ago

Last modified 16 months ago

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

[] is the syntax for attribute selectors

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/>');

comment:4 Changed 16 months 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.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.