Palmi Palmi - 5 months ago 14x
Javascript Question

Jquery get element not working when id contains comma

Why does the jquery selector not work when the id contains a comma?

<div id="1,3">Hello There!</div>

$(document).ready(function () {
var str = "#1,3";

if ($(str).length) {
} else {
alert('The element "'+str+'" does not exist on the document!');


You need to escape the comma with two backslashes:

var str = "#1\\,3";

jsFiddle example

See and

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\