Search code examples
jqueryjquery-mobileescapinghref

"Syntax error, unrecognized expression” error in jqm in href links


I've stumbled into this error. I'm dynamically creating the links to certain pages in JQM and using an <a> element.

For internal purposes, in my URIs I have colons breaking the jquery code. For instance a link is in the form aa-aa_bb-bb:cc:dd. In the jQuery documentation, I found that : causes an error inside the $ element and has to be escaped using a simple function. This is the article: http://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/

My code is not working when clicking on one of the links I create inside a JQM window. Why is this happening?


Solution

  • When you are travelling in the pages of a JQM site, you are travelling inside a single DOM, where all the pages are loaded inside one page and you are showing one id at a time.

    Hence, when you click on a link, you use internal JQM/jQuery mechanisms that need the ids to be escaped.

    I solved it by escaping the link href with the function used for the id selector in $ and described in http://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/

        '<a href="'+jq(link_href)+'">'
    

    Hope this can help!