I have an HTML which has some elements having ids with colons. For example,
<div id="i:have:colons">Get my selector!!</div>
<my:another:test> This time with tag names </my:another:test>
I want to select these elements using jQuery. Here are my few attempts and Jsbin Demo
function escape(id) {
return id.replace( /(:|\.|\[|\])/g, '\\$1');
}
var id = "i:have:colons";
// Does not work
console.log($('#' + id).length);
// Works
console.log($("#i\\:have\\:colons").length);
var escapedId = escape(id);
// Q1. Why answer shows only 1 backslash while I used 2 in regex
console.log(escapedId); //'i\:have\:colons'
// Works
console.log($('#' + escapedId).length);
// Q2. Does not work while escapedId === 'i\:have\:colons'. How and why ?
console.log($('#' + 'i\:have\:colons').length);
Edit After T.J Answer
var tag = 'my:another:test';
console.log('Testing tag name now----');
console.log($(tag).length);
var tag2 = tag.replace(/[:]/g, '\\\\:');
// Does not work with tagnames but this works with Id
console.log($(tag2).length);
var tag3 = tag.replace(/[:]/g, '\\:');
// Q3. Why does this work with tagnames but not with ids ?
console.log($(tag3).length);
My Question is in the comments in JS code.