Convert string to number and add one

JqueryHtmlJquery Ui

Jquery Problem Overview


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

Jquery Solutions


Solution 1 - Jquery

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

Solution 2 - Jquery

Have you tried flip-flopping it a bit?

var newcurrentpageTemp = parseInt($(this).attr("id"));
newcurrentpageTemp++;
alert(newcurrentpageTemp));

Solution 3 - Jquery

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

http://jsfiddle.net/GfqMM/

Solution 4 - Jquery

$('.load_more').live("click",function() { //When user clicks
          var newcurrentpageTemp = parseInt($(this).attr("id")) + 1
          dosomething(newcurrentpageTemp );
      });

http://jsfiddle.net/GfqMM/

Solution 5 - Jquery

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

Solution 6 - Jquery

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
          

Solution 7 - Jquery

You have to parse the id before adding 1

 $('.load_more').live("click",function() { //When user clicks
              var newcurrentpageTemp = parseInt($(this).attr("id"));
              newcurrentpageTemp ++;
              dosomething(newcurrentpageTemp );
 });

Solution 8 - Jquery

The simplest solution here is to change

  var newcurrentpageTemp = $(this).attr("id") + 1;//Get the id from the hyperlink

to:

  var newcurrentpageTemp = (($(this).attr("id")) * 1) + 1;//Get the id from the hyperlink

Solution 9 - Jquery

The parseInt solution is the best way to go as it is clear what is happening.

For completeness it is worth mentioning that this can also be done with the + operator

$('.load_more').live("click",function() { //When user clicks
  var newcurrentpageTemp = +$(this).attr("id") + 1; //Get the id from the hyperlink
  alert(newcurrentpageTemp);
  dosomething();
});

Solution 10 - Jquery

a nice way from http://try.jquery.com/levels/4/challenges/16 :

adding a + before the string without using parseInt and parseFloat and radix and errors i faced for missing radix parameter

sample

var number= +$('#inputForm').val();

Solution 11 - Jquery

var sVal = '234';
var iNum = parseInt(sVal); //Output will be 234.

http://www.jquerybyexample.net/2013/02/jquery-convert-string-to-integer.html

Solution 12 - Jquery

var first_value = '2';
// convert this string value into int
parseInt(first_value);

Solution 13 - Jquery

Simple and best solution is like this. take a string in one variable and then convert it using parseInt() method like below.

var stringValue = '921795';
var numValue = parseInt(stringValue);

parseInt() method will return the number like this 921795. after this, you can add any number to your value.

http://www.phpcodify.com/convert-string-to-integer-using-jquery-parseint/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNiklasView Question on Stackoverflow
Solution 1 - JqueryJustin NiessnerView Answer on Stackoverflow
Solution 2 - JqueryDoozer BlakeView Answer on Stackoverflow
Solution 3 - Jquerycyco130View Answer on Stackoverflow
Solution 4 - JquerySedat BaşarView Answer on Stackoverflow
Solution 5 - JqueryJayendraView Answer on Stackoverflow
Solution 6 - JqueryChris SnowdenView Answer on Stackoverflow
Solution 7 - JqueryJean-CharlesView Answer on Stackoverflow
Solution 8 - JqueryDevlshOneView Answer on Stackoverflow
Solution 9 - Jquerynjr101View Answer on Stackoverflow
Solution 10 - JqueryImanView Answer on Stackoverflow
Solution 11 - JquerybrilliantairicView Answer on Stackoverflow
Solution 12 - JqueryAli UmairView Answer on Stackoverflow
Solution 13 - JqueryEhtesham ShamiView Answer on Stackoverflow