0

I'm having an input like this.

<input id="test" value="150,000">

And I want get value from this input and perform calculations.

How can I convert value="150,000" into a number like this 150000?

I tried numberFormat($("input[id='test']").val()) but it not OK.

Alex
  • 727
  • 1
  • 13
  • 32
  • A mistake in the tag caused you to think this is that duplicate. It's not. It's a question in JQuery, probably a duplicate of some other question.... See my remark to the answer given. – pashute Aug 18 '19 at 04:41

1 Answers1

2

You can simply remove , and parse it to number

function getValue(){
  let inputValue = document.getElementById('test').value
  let number =  +inputValue.replace(/,/g,'')
  
  console.log(number)
  console.log(typeof number)
}
<input id="test" value="150,000">
<button onclick="getValue()"> Give numbers</button>
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • Obviously the question is in JQuery. The answer is to use parseInt: ```parseInt($("#test").val());``` – pashute Aug 18 '19 at 04:39
  • @pashute number with `,` cannot be parsed properly with parseInt, this is what the root cause of problem here, it doesn't make any difference if it is `jQuery` or `vanilla js` , try `console.log(parseInt("120,123"))` and see the result – Code Maniac Aug 18 '19 at 05:02
  • Oops. You're right of course. Use js Number() function. ```Number($("test").Val());``` – pashute Aug 19 '19 at 16:07