Advertisement
Convert string to number and add one
Question
I want to turn the value I get from the id into a number and add one to it then pass the new value into the dosomething()
function to use. When I tried this and the value is one I get back 11 not 2.
$('.load_more').live("click",function() { // When user clicks
var newcurrentpageTemp = $(this).attr("id") + 1;// Get id from the hyperlink
alert(parseInt(newcurrentpageTemp));
dosomething();
});
2013/05/23
Accepted Answer
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);
2011/10/06
Read more... Read less...
Have you tried flip-flopping it a bit?
var newcurrentpageTemp = parseInt($(this).attr("id"));
newcurrentpageTemp++;
alert(newcurrentpageTemp));
2011/10/06
I believe you should add 1 after passing it to parseInt
$('.load_more').live("click",function() { //When user clicks
var newcurrentpageTemp = parseInt($(this).attr("id")) + 1;
alert(newcurrentpageTemp);
dosomething();
});
2011/10/06
$('.load_more').live("click",function() { //When user clicks
var newcurrentpageTemp = parseInt($(this).attr("id")) + 1
dosomething(newcurrentpageTemp );
});
2011/10/06
Parse the Id as it would be string and then add.
e.g.
$('.load_more').live("click",function() { //When user clicks
var newcurrentpageTemp = parseInt($(this).attr("id")) + 1;//Get the id from the hyperlink
alert(newcurrentpageTemp);
dosomething();
});
2011/10/06
I've got this working in a similar situation for moving to next page like this:
$("#page_next").click(function () {
$("#pageNumber").val(parseInt($("#pageNumber").val()) + 1);
submitForm(this);
return false;
});
You should be able to add brackets to achieve what you want something like this:
var newcurrentpageTemp = (parseInt($(this).attr("id"))) + 1;//Get the id from the hyperlink
2011/10/06
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]