0

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

Notably, $(“<html>”) syntax now requires the first character to be ‘<’ to indicate an HTML template. So in places where you have whitespace, you can fix with a call to “trim”.

Although, As Slaks mentioned n according to me explicit call to $.parseHTML would fix it best.

1 Comment

trim() actually fixes the specific issue that I was having. I'd left those details out to keep the description simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.