Skip to main content

Bug Tracker

Side navigation

#10075 closed bug (invalid)

Opened August 17, 2011 03:13PM UTC

Closed August 18, 2011 03:20PM UTC

Last modified September 20, 2011 09:25AM UTC

$(" ... html ...") strips leading and trailing text nodes in HTML string

Reported by: phil127+jq@gmail.com Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.6.2
Keywords: neededdocs Cc:
Blocked by: Blocking:
Description

Hi,

I stumbled about this when trying to use $() with an HTML string that contains multiple top-level nodes and starts with a text node:

  • $("1<br/>2<br/>3")
  • Expected result: ["1", <br>, "2", <br>, "3"]
  • Observed result: [<br>, "2", <br>]

Additional observations:

The nodes are created as expected if the HTML string starts with "<" and ends with ">", even if the characters are converted to text nodes.

  • $("1<br/>2<br/>3 >") returns (incorrectly):
  • [<br>, "2", <br>, "3 >"]
  • $("< 1<br/>2<br/>3") returns (incorrectly):
  • ["< 1", <br>, "2", <br>]
  • $("< 1<br/>2<br/>3 >") returns (correctly):
  • ["< 1", <br>, "2", <br>, "3 >"]
  • $("1<br/>2<br/>3<!-- -->") returns (incorrectly):
  • [<br>, "2", <br>, "3", (comment)]
  • $("<!-- -->1<br/>2<br/>3") returns (incorrectly):
  • [(comment), "1", <br>, "2", <br>]
  • $("<!-- -->1<br/>2<br/>3<!-- -->") returns (correctly):
  • [(comment), "1", <br>, "2", <br>, "3", (comment)]

(The last is what I'm using as a workaround right now.)

Tested on:

  • Firefox 6.0
  • Google Chrome 13.0.782.112 m

OS:

  • Windows 7 x64 SP1

Regards,

Philipp Serafin

Attachments (0)
Change History (2)

Changed August 18, 2011 03:20PM UTC by dmethvin comment:1

keywords: → needsdocs
resolution: → invalid
status: newclosed

jQuery isn't intended to process text nodes as if they were DOM elements. Most methods save a few like .content() remove or ignore them if they are in a jQuery collection.

I've marked the ticket for a documentation review, but the behavior is correct.

Changed September 20, 2011 09:25AM UTC by addyosmani comment:2

keywords: needsdocsneededdocs

I've updated the docs for this here: http://api.jquery.com/jQuery/