-4

How can I strip the $ and , characters from the following value, the code i am using is not working

var asset_value =   second_col.find("input[type=text]").val().replace('$', '');

second_col.find("input[type=text]").val() looks like

$1,080.00

Update:

I am not sure why i am getting voted down! the duplicate solution does not solve my question, nor does any answer below, except for this, very strange!

second_col.find("input[type=text]").val(function(i, val) {
    return val.replace(/\$|,/g, '');
});

var asset_value =   second_col.find("input[type=text]").val();
Ossama
  • 2,401
  • 7
  • 46
  • 83
  • `'$1,080.00'.replace(/[\$,]/g, '')` should work. – thefourtheye Jan 16 '18 at 05:36
  • 2
    note, you don't need to escape `$` when it's inside `[]` – Jaromanda X Jan 16 '18 at 05:37
  • 2
    Please define "_doesn't work_". What exactly you expect to get? What you get instead? Errors in the console? Your code [works as it is](https://jsfiddle.net/hx5jjck4/). – Teemu Jan 16 '18 at 05:37
  • '$1,080.00'.replace(/[$,]/g, ''); – Dyrandz Famador Jan 16 '18 at 05:43
  • 1
    Try `second_col.find("input[type=text]").val(function(i, val) { return val.replace(/\$|,/g, ''); });`. – Tushar Jan 16 '18 at 05:45
  • this is the only solution that seems to work, put htis as an answer – Ossama Jan 16 '18 at 05:50
  • I am not sure why i am getting voted down! the duplicate solution does not solve my question, nor does any answer below, except for this, very strange! I would not ask such question unless it is the last resort – Ossama Jan 16 '18 at 23:38
  • It looks like you're expecting the value of the input to change. Why didn't you ask it? You're presenting a snippet, where you assign a replaced value to a variable, there's not a single word about you want to change the value of the input in your question. Hence the question is unclear and not useful, that's why you've got down votes. – Teemu Jan 18 '18 at 08:37

1 Answers1

0

You can use regex for this:

second_col.find("input[type=text]").val().replace(/[\$,]/g,'').

Assign this value to variable and use it.

Saurabh Verma
  • 483
  • 1
  • 4
  • 17