Search code examples
jqueryspaceprefix

jQuery cannot parse HTML strings with prefixed white space?


Whether it be a tab space, one regular space or many, jQuery cries Uncaught Error: Syntax error, unrecognized expression: <div></div>

Here's the code in jsFiddle: http://jsfiddle.net/dc8fK/

Here's the code:

console.log( $("<div></div>") );    // works
console.log( $(" <div></div>") );   // not

Aside from searching and removing any prefixed white space, is there a better, more official way of dealing with this?


Solution

  • You should call $.parseHTML(), which can parse arbitrary HTML strings.