0

I am getting a value from a div :

<div id="test">100</div>

And what I want to do is to get the value with jQuery, raise with 50% and show the value.

$number= $('#test').text() //getting the value, without parseInt or parseFloat?
$newNumber = $number * 0,5 //show the result of this

I often end up with NaN, so I'd like to have a guidance on that. Thank you.

13
  • Use parseFloat(): var $number = parseFloat($('#test').text(), 10) Commented May 11, 2014 at 13:31
  • @arnaud576875 * will automatically parse it to number Commented May 11, 2014 at 13:33
  • @A.Wolff just because you can doesn't mean you should Commented May 11, 2014 at 13:34
  • @arnaud576875 Itsn't at all, but using an useless parameter is, at the opposite, ugly, parseFloat accepting only one parameter ;) Commented May 11, 2014 at 13:36
  • @A.Wolff oops, old parseInt() habits Commented May 11, 2014 at 13:37

2 Answers 2

2

Suppose we have following markups:

<div id="test">100</div>
<div id="result"></div>

what you are looking for can be achieved as following (without using parseInt() or parseFloat() functions)

var number = $('#test').text();
var result = Number(number) + (number * 0.5);
$('#result').html(result);
Sign up to request clarification or add additional context in comments.

Comments

2

You have error in your code, just fix it with:

$number * 0.5

Using dot, not comma.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.