JQuery add attribute with backslash

I am trying to add an input dynamically, with a custom html5 validation. The jQuery code is below.

$('#test').html('<input type="text" name="test_input" id="test_input" required pattern="\d*" title="Please Enter Valid No.">');

      

but in html file looks like this:

<input type="text" name="test_input" id="test_input" required pattern="d*" title="Please Enter Valid No.">

      

instead \d*

goes d*

.

+3


source to share


2 answers


Escape the slash \

:

$('#test').html('<input type="text" name="test_input" id="test_input" required pattern="\\d*" title="Please Enter Valid No.">');

      

or you can also write the symbol code for \

ie &#92;

, like:



$('#test').html('<input type="text" name="test_input" id="test_input" required pattern="&#92;d*" title="Please Enter Valid No.">');

      

DEMO

+2


source


Escaped, i.e. \



$('#test').html('<input type="text" name="test_input" id="test_input" required pattern="\\d*" title="Please Enter Valid No.">');

      

+2


source







All Articles