JQuery adding number to text

I am trying to do something, although it would be very simple. add the value 1 to the number.

However, the number was plain text, and things got a little confusing. As you can guess, I'm pretty new to this.

My goal is to add 1 point to the existing rating, the rating has one decimal place, for example 1.3. Thus, the desired result after adding a point is 2.3. However, the current script I wrote returns adds a dot to the second decimal place, and I don't understand why.

Thanks for the help.

var getPoints = parseInt($('.awesome-points').text(), 10).toFixed(1);
alert(getPoints);
var addPoint = 1;
var newScore = getPoints + addPoint;
$('.awesome-points').text(newScore);

Mark

+3
source share
2 answers

.toFixed()returns a string (see the MDN documentation ), so it getPoints + addPointconcatenates strings, not appends.

:

// unary + converts any numerical string to a number (no matter whether float
// or int)
var getPoints = +$('.awesome-points').text();
var newScore = getPoints + 1;
$('.awesome-points').text(newScore.toFixed(1));

:

$('.awesome-points').text(function(i, val) {
    return (+val + 1).toFixed(1);
});

:

.text(), . . , , , - . .
.text().

(+val + 1).toFixed(1) : val (+val), (+ 1). .toFixed(1) .

+val NaN, . , ,

return ((+val || 0) + 1).toFixed(1); 

, , +val false ( NaN), 0. JavaScript, . "options = options || {}" Javascript?.

+9

, -

var getPoints = $('.awesome-points').text();
//alert(getPoints);
var addPoint = 1;
var newScore = parseFloat(getPoints) + addPoint;
$('.awesome-points').text(newScore.toFixed(1));

, , toFixed() , , toFixed

0

All Articles