After omitting $ from input getting NaN in javascript asp.net
In javascript, I have given开发者_StackOverflow社区 like this,( edited adding Pasman response )
var y= document.getElementById('<%= DataItemValue4.ClientID%>').firstChild.nodeValue.replace('$', '');
alert(Number(y));
y = y *48;
the value I am reading here is integer type amount with $, ex: $200, $10 etc.,
and after omitting $
symbol, i get ex: 200, 10
. Now before i do arithmetic operations on y, alert shows me value as NaN,even if i don't write Number() also i am getting same problem. how to get numerical y and able to perform arithmetic operation on it.
You can replace the $ sign like this:
var y = document.getElementById('<%= DataItemValue4.ClientID%>')
.firstChild.nodeValue.replace('$','');
If the string is always exactly '$xxx'
(i.e. no spaces etc), then you just take the substring (with e.g. substr
):
val = val.substr(1);
If there are spaces, you have to use regular expressions (basically trimming the value):
val = val.replace(/^[\s$]+|[\s]+$/g, '');
精彩评论