Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#7923 closed bug (duplicate)

Documentation bug: .text() does not always return CDATA

Reported by: [email protected] Owned by:
Priority: undecided Milestone: 1.next
Component: manipulation Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:


http://bugs.jquery.com/ticket/6827 was closed wontfix ("use .html()").

However, the documentation (http://api.jquery.com/text/) says .text() returns CDATA, which it doesn't -- apparently by design.

Either 6827 should be fixed, or the documentation should reflect the SCRIPT element restriction.

Change History (4)

comment:1 Changed 12 years ago by [email protected]

Here's a jsFiddle just for completeness. No output on IE8.


comment:2 Changed 12 years ago by jitter

Component: unfiledmanipulation
Resolution: duplicate
Status: newclosed

Thanks for taking the time to contribute to the jQuery project by writing a bug report.

As the (I admit that it's rather scarce) closing text for #6827 states, you should use .html() instead of .text() to retrieve the content of script tag. This test case works for me in IE.

The documentation on .text() is correct in stating that it returns the value of CDATA nodes. But in this case there is no CDATA node just a script tag which is a Element node and this Element node (in all browser but IE) has a single child node which is a text node.

comment:3 Changed 12 years ago by jitter

Duplicate of #6827.

comment:4 Changed 12 years ago by anonymous

And thanks for your quick response!

The "use .html()" text is so scarce that you didn't see I had it in the original description. :)

The .text() method is documented to "get the combined text contents of each element". It returns "". If the documentation is correct, that must mean the script tag and its descendants are considered to have no text contents at all, which is unexpected (at least to me). That it differs across browsers is even more unexpected.

I filed a documentation bug because I didn't expect to have to go find a closed bug report to discover that this inconsistency exists and is apparently by design. (Though really, as a cross-browser framework this seems like exactly the sort of thing jQuery should make consistent across browsers.)

Note: See TracTickets for help on using tickets.