Syed Waqas Bukhary
Syed Waqas Bukhary

Reputation: 5340

Uncaught Error: Syntax error, unrecognized expression: with double equal in selector ==

I have office 365 id on message divs. such as

<div id="AQMkADAwATM0MDAAMS0wYWIxLTU1ADI4LTAwAi0wMAoALgAAA1IXzflHRQlLlY3LIdjzH3MBADg_s4AQY3NEqDFmBjvfdZIAAAIBDwAAAA==">Message Subject</div>

But when I call it in functions. It gives jquery-2.1.1.js:2 Uncaught Error: Syntax error, unrecognized expression:

This works on devtools console.

$('#AQMkADFGFGDFGDFGwYWIxLTU1ADI4LTAwAi0wMAoALgAAA1IXzflHRQlLlY3LIdjzH3MBADg_s4AQY3NEqDFmBjvfdZIAAAIBDwAAAA')

This does not work on devtools console. I want to fix this because office 365 messages ids usually have double equal in the end ==.

 $('#AQMkADFGFGDFGDFGwYWIxLTU1ADI4LTAwAi0wMAoALgAAA1IXzflHRQlLlY3LIdjzH3MBADg_s4AQY3NEqDFmBjvfdZIAAAIBDwAAAA==')

Upvotes: 2

Views: 1913

Answers (2)

Sphinx
Sphinx

Reputation: 10729

The root cause is jQuery uses CSS syntax for selecting elements.

You can use one regex expression to escape with double backslashes for an ID that has characters used in CSS notation.

console.log($("#"+"AQMkADAwATM0MDAAMS0wYWIxLTU1ADI4LTAwAi0wMAoALgAAA1IXzflHRQlLlY3LIdjzH3MBADg_s4AQY3NEqDFmBjvfdZIAAAIBDwAAAA==".replace( /(:|\.|\[|\]|,|=|@)/g, "\\$1" )).text())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="AQMkADAwATM0MDAAMS0wYWIxLTU1ADI4LTAwAi0wMAoALgAAA1IXzflHRQlLlY3LIdjzH3MBADg_s4AQY3NEqDFmBjvfdZIAAAIBDwAAAA==">Message Subject</div>

Upvotes: 4

BoltClock
BoltClock

Reputation: 724172

  1. If the structure is not known to change, save yourself a headache and select by structure instead of ID.

  2. If you must select by ID, either use an attribute selector:

    $('[id="AQMkADFGFGDFGDFGwYWIxLTU1ADI4LTAwAi0wMAoALgAAA1IXzflHRQlLlY3LIdjzH3MBADg_s4AQY3NEqDFmBjvfdZIAAAIBDwAAAA=="]')
    

    or escape the equals signs:

    $('#AQMkADFGFGDFGDFGwYWIxLTU1ADI4LTAwAi0wMAoALgAAA1IXzflHRQlLlY3LIdjzH3MBADg_s4AQY3NEqDFmBjvfdZIAAAIBDwAAAA\\=\\=')
    

Upvotes: 6

Related Questions