1

Im using angular and jquery to scroll to an element base on his location hash string. In my situation i need to include in the string the '?' char, but its seems like jquery has problem with this.

This is the link:

<a href="#/faq#whenAreLotteryResultsUpdatedOnTheSite?">when Are Lottery Results Updated OnThe Site </a>

This is the jquery code:

var elem = '#' + $location.hash();
console.log($(elem));

The error:

Error: Syntax error, unrecognized expression: #whenAreLotteryResultsUpdatedOnTheSite?

Any solution?

1
  • Are you forgetting $() or something like that? Commented Feb 8, 2015 at 8:52

1 Answer 1

1

Yes, jQuery will refuse to select elements with special characters in CSS selector. You just need to escape them with \\:

var elem = $location.hash().replace(/\?/, '\\\\?');

This will properly escape ? character.

Also note that location.hash will already include leading # so you don't need to prepend one more.

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

3 Comments

I tried your code and im getting this: #whenAreLotteryResultsUpdatedOnTheSite\\? and in angular function the hash does not include
Yes, this is what you need, you need to escape ? with double `` so jQuery could use it.
Error: Syntax error, unrecognized expression: #whenAreLotteryResultsUpdatedOnTheSite\\? when i do this console.log($(elem))

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.