Search code examples
jquerysizzle

Creating a selector pointing to raw text


Following up on https://github.com/Semantic-Org/Semantic-UI/issues/886#issuecomment-46210448, I'm trying to return a jQuery selector containing raw text (no HTML tags), but if I do $("Error: something has gone wrong") I get:

Uncaught object jquery.js:1437
Sizzle.error jquery.js:1437
Sizzle.tokenize jquery.js:2051
Sizzle.select jquery.js:2452
Sizzle jquery.js:843
jQuery.fn.extend.find jquery.js:2668
jQuery.fn.init jquery.js:2776
jQuery jquery.js:76
$.fn.form.settings.templates.error Forms.js:31
$.fn.form.$allModules.each.module.add.errors semantic.js:1638
$.fn.form.$allModules.each.module.invoke semantic.js:1897
(anonymous function) semantic.js:1918
jQuery.extend.each jquery.js:375
jQuery.fn.jQuery.each jquery.js:139
$.fn.form semantic.js:1375

Wrapping the text in a <span> makes the exception go away, but is there a better way?


Solution

  • Using the jQuery function with a raw non-HTML string doesn't make sense.

    http://api.jquery.com/jQuery/#jQuery2

    According to this documentation, the jQuery() function parses HTML, and not XML, and by extension not even raw text.

    If you need to append some raw text to an element use the .text() method.

    example:

    $('.someClass').text('Error: Message');