jQuery selectors and backslashes -


i have dom element contains qualified name part of id attribute;

<div id="domain\element\div">my div</div> 

it seems impossible jquery select element id. here's experiment;

var $e1 = $('#domain\\\element\\\div'); var $e2 = $('#domain\\element\\div'); var $e3 = $(document.getelementbyid('domain\\\element\\\div')); console.log($e1); console.log($e2); console.log($e3); 

the output of console displays first 2 empty while third works;

[] [] <div id=​"domain\element\div">​todo write content​</div>​ 

i using jquery 1.5.2. bug jquery or selector strings wrong?

if can change id within html code, find other separator backslash \ id can make valid id selector (see here). underscore _ good.

if can't alter html, jquery can still work backslashes in ids , id selectors. except, you'll need use 4 backslashes match each literal backslash in id selector:

$('#domain\\\\element\\\\div') 

you achieve by

  1. taking id:

    domain\element\div 
  2. adding # symbol , escaping backslashes selector:

    #domain\\element\\div 
  3. escaping each pair of backslashes use in javascript strings doubling them (also notice quotes):

    '#domain\\\\element\\\\div' 
  4. then passing string $() above.

jsfiddle


Comments