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.
Answer from Rory McCrossan on Stack OverflowIt 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.
You can use parseInt(string, radix) to convert string value to integer like this code below
var votevalue = parseInt($('button').data('votevalue'));
DEMO
The parseInt() function of JS parses a string and returns an integer.
Ex:
var a = parseInt("10");
Your code is not of javascript.
<% int ai= Integer.parseInt(beginningBalance.get(i));%>
it looks like ASP.net if you want pure javascript or jquery then answer is same as provide above.
var a = parseInt("10");
so you can change your code in javascript like this:
var a = parseInt( '<% beginningBalance.get(i); %>');
That is because string.split returns an array of strings.
If you want to handle each individual value returned by split, loop over the array and parse the items as numbers while iterating.
You can then do with the parsed number as you will. (in this example, it multiplies every number by two and appends the output)
var a = '1 - 2 - 3 - 4 - 5 - 6';
var splitarray = a.split('-')
for(var i=0; i < splitarray.length; i++)
{
var valueAsInt = parseInt(splitarray[i]);
//do whatever you want with valueAsInt, like for instance
valueAsInt *= 2;
//adding the br so we can see what the individual numbers are
$('#resultDiv').append(valueAsInt + "<br />");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="resultDiv"></div>
parseInt will convert a single string, not an array of strings.
You can use jquery $.each to parse each item and return an array of int.
(putting this into html as in the question and the snippet below doesn't really mean much as it will convert back to string for the html, but the values can be manipulated once in the array).
var a = '1 - 2 - 3 - 4 - 5 - 6';
var arr = $.each(a.split('-'), function() { return parseInt(this, 10); });
var a = '1 - 2 - 3 - 4 - 5 - 6';
var b = $.each(a.split('-'), function() { return parseInt(this, 10); });
// b is now an array of ints
$("#result").html(b.join(","))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='result'>
</div>
You need to parse string to integer. use parseInt(). it will parse string to integer.
replace this:
daysRest = $('#daysRest').val();
by
daysRest = parseInt($('#daysRest').val());
working Demo
Just needed a foreach loop in PHP and so i moved all the above there - works fine now. Thanks.
Assuming you are correct and your id is a proper number (without any other text), you should parse the id and then add one to it:
var currentPage = parseInt($(this).attr('id'), 10);
++currentPage;
doSomething(currentPage);
Have you tried flip-flopping it a bit?
var newcurrentpageTemp = parseInt($(this).attr("id"));
newcurrentpageTemp++;
alert(newcurrentpageTemp));
If you have single , inside string use replace:
string = parseInt(string.replace(',', ''), 10);
NOTE: replace will only replace the first occurrence of ,.
If you have multiple , inside string use regex in replace:
string = parseInt(string.replace(/,/g, ''), 10);
parseInt to convert string to integer.
Demo
Try this:
var string = "123,123";
var str = parseInt(string.replace(",",""));
JSFIDDLE DEMO