Reputation: 343
According to this page this should work. Here is the code and the JSFiddle.
<input id="id.docType" value="45"/>
<br/>
<p></p>
<input id="thevalue" />
var str = 'id.docType';
str = str.replace('.', '\\\\.');
var selector = '#' + str;
$('p').text(selector);
var x = $(selector).val();
$('#thevalue').val(x);
Any ideas why this doesn't work? I have ids that have periods and trying to use them as a selector with jQuery. jQuery's page says I should be able to escape the period with 2 back slashes but it isn't working.
Upvotes: 0
Views: 431
Reputation: 208040
Change
str = str.replace('.', '\\\\.');
to
str = str.replace('\.', '\\.');
Upvotes: 3
Reputation: 37781
The slash is double escaped, it only needs escaped once:
str = str.replace('.', '\\.');
Upvotes: 2