Search code examples
jquerycross-browser

Escaping a CSS selector


I've escaped a CSS selector like so:

$('#a\\[b\\]-c')

The [ and ] aren't valid characters, so I escaped them with 2 slashes. Seems to work just fine, but one of my clients is reporting that something isn't working on that page in IE7, and I can't reproduce it.

Is this escaping handled by jQuery internally, or does it delegate it back to the browser, and maybe their version of the browser isn't handling it correctly?

That's all I can think of....everything else looks fine.

Can't show source unfortunately.


Solution

  • That escaping is correct. The following will successfully target the <div>.

    console.log(document.querySelector('#a\\[b\\]-c').innerText)
    <div id="a[b]-c">target me</div>