kombat
kombat

Reputation: 343

jQuery escape period in id

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

Answers (2)

j08691
j08691

Reputation: 208040

Change

str = str.replace('.', '\\\\.');

to

str = str.replace('\.', '\\.');

jsFiddle example

Upvotes: 3

Douglas
Douglas

Reputation: 37781

The slash is double escaped, it only needs escaped once:

str = str.replace('.', '\\.');

Upvotes: 2

Related Questions