Convert string to number and add one

Niklas picture Niklas · Oct 6, 2011 · Viewed 308.5k times · Source

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();
});

Answer

Justin Niessner picture Justin Niessner · Oct 6, 2011

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);