[jquery] Convert string to number and add one

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

This question is related to jquery html jquery-ui

The answer is


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

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


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

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

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

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

Have you tried flip-flopping it a bit?

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

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

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

http://jsfiddle.net/GfqMM/


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

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

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/


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/


Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to jquery-ui

How to auto adjust the div size for all mobile / tablet display formats? jQuery not working with IE 11 JavaScript Uncaught ReferenceError: jQuery is not defined; Uncaught ReferenceError: $ is not defined Best Practice to Organize Javascript Library & CSS Folder Structure Change table header color using bootstrap How to get HTML 5 input type="date" working in Firefox and/or IE 10 Form Submit jQuery does not work Disable future dates after today in Jquery Ui Datepicker How to Set Active Tab in jQuery Ui How to use source: function()... and AJAX in JQuery UI autocomplete