I am facing problem with getting value of an element whose id
contains special character as .
and $
sign.
my id to element is "XS_19MAY2016_012720_311.04$_cal"
I am using syntax of jQuery as:
$("#"+xyz+"_cal").val() where xyz is variable having above id.
I am getting error as:
Error: Syntax error, unrecognized expression: #XS_19MAY2016_012720_311.04$_cal.
What I doing wrong or what I need to do to correct it.
Just escape the characters:
var foo = 'XS_19MAY2016_012720_311.04$';
$('#' + foo.replace(/[.$]/g, '\\$&') + '_cal').text('foo')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="XS_19MAY2016_012720_311.04$_cal"></div>
You can use this way to do that (without symbol escaping).
$(function() {
var
id = "XS_19MAY2016_012720_311.04$_cal",
text;
text = $('[id="' + id +'"]').text();
alert(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="XS_19MAY2016_012720_311.04$_cal">Yay!</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With