Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
172
rated 0 times [  175] [ 3]  / answers: 1 / hits: 42230  / 12 Years ago, mon, november 19, 2012, 12:00:00

I am using a function where I have a readonly text input, and when I execute the function I want the number value + 1. So let's say I have 60, when I execute the function, the number returned should be 61.



But instead it's coming out 601, which is just adding the number 1 to the string. Any clue as to what is going on? Subtraction, multiplication and division all work fine. Here is a snippet



 var num= $(#originalnum).val() + 1;
$(#originalnum).val(num);


And yes i've tried a few different variations, am I missing something?


More From » jquery

 Answers
7

A simple unary + is sufficient to turn a string into a number in this case:



var num = +$(#originalnum).val() + 1;
$(#originalnum).val(num);

[#81904] Sunday, November 18, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
madelyn

Total Points: 449
Total Questions: 100
Total Answers: 100

Location: Seychelles
Member since Fri, May 7, 2021
3 Years ago
madelyn questions
Wed, Jul 28, 21, 00:00, 3 Years ago
Wed, Jul 14, 21, 00:00, 3 Years ago
Sat, Nov 7, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;