Been trying to convert the following to number:
<button class="btn btn-large btn-info" data-votevalue="1"> <strong>1</strong> </button>
var votevalue = parseInt($(this).data('votevalue'));
I've also tried Number()
but I'm still getting NaN
when checking the result. What am I doing wrong?
Here is the complete code:
<div class="span7" id="button-group"> <div class="btn-group"> <button class="btn btn-large btn-info" data-votevalue="1"><strong>1</strong></button> <button class="btn btn-large btn-info" data-votevalue="2"><strong>2</strong></button> <button class="btn btn-large btn-info" data-votevalue="3"><strong>3</strong></button> <button class="btn btn-large btn-info" data-votevalue="4"><strong>4</strong></button> <button class="btn btn-large btn-info" data-votevalue="5"><strong>5</strong></button> <button class="btn btn-large btn-info" data-votevalue="6"><strong>6</strong></button> <button class="btn btn-large btn-info" data-votevalue="7"><strong>7</strong></button> <button class="btn btn-large btn-info" data-votevalue="8"><strong>8</strong></button> <button class="btn btn-large btn-info" data-votevalue="9"><strong>9</strong></button> <button class="btn btn-large btn-info" data-votevalue="10"><strong>10</strong></button> </div> </div>
$('#button-group button').each(function() { $(this).click(function() { $(this).addClass('active'); var votevalue = parseInt($(this).data('votevalue')); var filename = $('.mainimage').data('filename'); var votes = parseInt($('.mainimage').data('numvotes')); var totalscore = parseInt($('.mainimage').data('totalscore')); $.ajax({ type: 'POST', url: 'index.php/?category=vote', data: { "votevalue": votevalue, "filename": filename }, success: function() { votes++; alert(votes); var average = ((totalscore + votevalue) / votes); $('#vote-incremenet').html(votes); $('#display-average').html(average); $('#display-average').show(); $('#button-group button').each(function(){ $(this).unbind('click'); }); } }); // end ajax }); // end click }); // end each
How to convert a string to a number in JavaScript using the parseInt() function. Another way to convert a string into a number is to use the parseInt() function. This function takes in a string and an optional radix. A radix is a number between 2 and 36 which represents the base in a numeral system.
You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System. Convert class. It's slightly more efficient and straightforward to call a TryParse method (for example, int.
Description. The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN . If not NaN , the return value will be the integer that is the first argument taken as a number in the specified radix .
The parseInt method parses a value as a string and returns the first integer. A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10.
It sounds like this
in your code is not referring to your .btn
element. Try referencing it explicitly with a selector:
var votevalue = parseInt($(".btn").data('votevalue'), 10);
Also, don't forget the radix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With