Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change an HTML input value's data type to integer?

I'm using jQuery to retrieve a value submitted by an input button. The value is supposed to be an integer. I want to increment it by one and display it.

// Getting immediate Voting Count down button id
var countUp = $(this).closest('li').find('div > input.green').attr('id');
var count = $("#"+countUp).val() + 1;
alert (count);

The above code gives me a concatenated string. Say for instance the value is 3. I want to get 4 as the output, but the code produces 31.

How can I change an HTML input value's data type to integer?

like image 404
ptamzz Avatar asked Mar 16 '11 15:03

ptamzz


People also ask

How do you input integers in HTML?

The <input type="number"> defines a field for entering a number. Use the following attributes to specify restrictions: max - specifies the maximum value allowed.

How do you make an input field only take numbers?

By default, HTML 5 input field has attribute type=”number” that is used to get input in numeric format. Now forcing input field type=”text” to accept numeric values only by using Javascript or jQuery. You can also set type=”tel” attribute in the input field that will popup numeric keyboard on mobile devices.

How do you make an HTML input tag only accept numeric values?

You can use the <input> tag with attribute type='number'. This input field allows only numerical values. You can also specify the minimum value and maximum value that should be accepted by this field.


2 Answers

To convert strValue into an integer, either use:

parseInt(strValue, 10);

or the unary + operator.

+strValue

Note the radix parameter to parseInt because a leading 0 would cause parseInt to assume that the input was in octal, and an input of 010 would give the value of 8 instead of 10

like image 86
Alnitak Avatar answered Sep 17 '22 17:09

Alnitak


parseInt(  $("#"+countUp).val()  ,  10  )
like image 33
Quentin Avatar answered Sep 19 '22 17:09

Quentin