Bug Tracker

Opened 12 years ago

Closed 9 years ago

Last modified 8 years ago

#2425 closed bug (invalid)

.text() and CDATA

Reported by: john Owned by: john
Priority: low Milestone:
Component: core Version: 1.4.3
Keywords: Cc:
Blocked by: Blocking:


Currently accessing CDATA contents can be pretty annoying across platforms (IE is different from the rest. .text() should be made to handle these differences.

Change History (11)

comment:1 Changed 12 years ago by Markus.Staab

+1 there should be a crossbrowser solution

comment:2 Changed 12 years ago by raccettura

I've just re-encountered this. For the curious my workaround is instead of using .text() to use this plugin I created (simply replace .text() with .getCDATA():

jQuery.fn.getCDATA = function() {
		return this[0].childNodes[0].nodeValue;

	// Other browsers do this
	return this[0].childNodes[1].nodeValue;

It ain't pretty, but in my case did the job.

comment:3 Changed 12 years ago by dan_nl

Are you specifying the dataType in your ajax call? If specified as xml .text() should get you the node text within the CDATA; same goes for the .get method.


type: "GET", url: "your.xml", dataType: "xml" success: your_function


comment:4 in reply to:  3 Changed 12 years ago by raccettura

I'm not specifying a type. The response from the server is application/xml, so jQuery should be treating it as such.

Per the docs:

The type of data that you're expecting back from the server. If none is specified, jQuery will intelligently pass either responseXML or responseText to your success callback, based on the MIME type of the response

Based on this, jQuery should correctly handle this.

Now I'm wondering if the bug is in jQuery's detection methods or in it's ability to parse CDATA.

comment:6 Changed 10 years ago by Rick Waldron

Keywords: needsreview added
Milestone: 1.2.4
Owner: set to john
Status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a jsFiddle that outlines practical use cases for your enhancement request.

comment:7 Changed 10 years ago by Rick Waldron

Keywords: needsreview removed
Priority: majorlow

comment:8 Changed 10 years ago by andrew.murphy@…

Doesn't work for me.

When fetching XML, our server doesn't send the right mime-type,

  • if I specify datatype, needs to be browser specific (FF, IE) : 'xml' or 'text/xml' get it the wrong way round => barfs
  • neither way parses CDATA correctly...
  • whichever datatype is specified, (or if I don't pass the datatype field) data is treated as HTML
  • - which works fine... until a CDATA field (which is bad HTML)


  • respect the datatype field, and make it cross browser (i.e. ignore server mime-type if datatype is set)


  • use datatype plain text
  • then convert using "normal" JS

if ( window.DOMParser ) {var parser = new DOMParser(); xml = parser.parseFromString( data, "text/xml" );} else

{ xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = "false"; xml.loadXML( data ); }

Which leads to a: Feature Request

  • add: xml_to_text() and text_to_xml()

comment:9 Changed 10 years ago by trac-o-bot

Resolution: invalid
Status: pendingclosed

Because we get so many tickets, we often need to return them to the initial reporter for more information. If that person does not reply within 14 days, the ticket will automatically be closed, and that has happened in this case. If you still are interested in pursuing this issue, feel free to add a comment with the requested information and we will be happy to reopen the ticket if it is still valid. Thanks!

comment:10 Changed 9 years ago by andrew.murphy@…

Bug mentioned in comment #8 happened in version 1.4.3.

Still valid.

comment:11 Changed 9 years ago by danheberden

Resolution: invalid
Status: closedreopened

I'd like to see a test case of this with the latest build of jquery

comment:12 Changed 9 years ago by snover

Resolution: invalid
Status: reopenedclosed

Please reopen/comment on this ticket only if you have a valid test case for the latest version of jQuery.

Note: See TracTickets for help on using tickets.