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 \
, like:
$('#test').html('<input type="text" name="test_input" id="test_input" required pattern="\d*" title="Please Enter Valid No.">');
+2
source to share