simple guy
simple guy

Reputation: 673

Can't get '.' when it is the last character in input type number

How do i retrieve the text value of my input type number?

I can retrieve the value just fine when it's a valid number

1.23 //returns 1.23
1 //returns 1

But when the last character is '.'. I can't seem to get it.

2. //returns 2

Want it to return 2.

HTML:

<input type="number" id="size" step="0.001"></td>

JS:

$("#size").val();

Upvotes: 0

Views: 268

Answers (3)

Saurabh Tiwari
Saurabh Tiwari

Reputation: 5161

With the type="number" you probably get a numbered version of the input value.

If you try Number('13.') in Javascript you get 13

Change it to type="text" and you should get the entire value.

Upvotes: 1

Ali Nm
Ali Nm

Reputation: 31

Check for '.' using string functions. If it does not exist, then append it. Else you have to use input type text.

Upvotes: 0

Emad Emami
Emad Emami

Reputation: 6639

use this:

parseFloat($("#size").val());

all results:

parseFloat('1.23') // return 1.23;
parseFloat('1') // return 1;
parseFloat('2.') // return 2;

Upvotes: 0

Related Questions