Click here to Skip to main content
15,886,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I make: "var id1 = $("#text1").val();" an integer in jQuery and Javascript?
C#
$(function () {
    $("#btn1").click(function () {
        var id1 = $("#text1").val();
    });
});
Posted
Updated 26-Feb-15 2:10am
v2
Comments
Pradip R 26-Feb-15 8:10am    
You mean you want to allow only integer values in textbox only?
enhzflep 26-Feb-15 8:15am    
Any reason to use a text input - <input type='text'/> instead of a number input - <input type='number' /> ?
Using a number input does the work for you and gives you increment/decrement buttons too. You can also make use of the min and max attributes of a number input to limit its range.
teledexterus 26-Feb-15 8:25am    
<input type=text> always returns a string. How do you get an integer in jQuery?
enhzflep 26-Feb-15 8:35am    
By using <input type='number'/> or by following Member10004231's suggestion and making use of ParseInt.
e.g
var numString = "10";
var num = ParseInt(numString);


<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
function byId(e){return document.getElementById(e);}

window.addEventListener('load', onDocLoaded, false);

function onDocLoaded()
{
byId('dblBtn').addEventListener('click', onDblBtnClick, false);
}

function onDblBtnClick(evt)
{
var val = byId('numInput').value * 2;
alert(val);
}
</script>
<style>
</style>
</head>
<body>
<input type='number' min='0' max='10' value='5' id='numInput'/><button id='dblBtn'>x 2</button>
</body>
</html>

1 solution

use parseInt() to convert the value in integer.
example:
var intValue=parseInt(id1);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900