Ilia Choly
Ilia Choly

Reputation: 18557

jQuery escaping <style> content

I'm using jquery as an xml parser (big mistake). However, I have too much invested to switch now. I'm having issues with jQuery automatically escaping the <style> tag's content.

var a = $("<style><foo>content</foo></style>"),
    b = $("<bar><foo>content</foo></bar>");

b.find('foo').length // => 1
a.find('foo').length // => 0

b.html() // => '<foo>content</foo>'
a.html() // => 'foo>content</foo>'
         //     ^--- missing '<'

b.text() // 'test'
a.text() // 'foo>content</foo>'

Is there a way to prevent jQuery from doing this?

Upvotes: 1

Views: 69

Answers (1)

user1726343
user1726343

Reputation:

You could use:

var xml = jQuery.parseXML("<style><foo>content</foo></style>");
var a = $(xml);

in order to prevent jQuery from treating the style tag differently.

Here is a demonstration: http://jsfiddle.net/FABmh/

Upvotes: 2

Related Questions