0

This might be a very simple quesion. But I simply haven't been able to find the answer googling around.

I was wondering if there was a way that I could get the int value of a textfield instead of just the value.

Html:

<input id="Price" type="number" value="" placeholder="Price" />

JavaScript:

var oCar = 
{
    "Price": $('#Price').val()
}

Let's fx. say that I then add a speed property to the car. And later, I would then like to call it in an animate function like = "oCar.Speed".

$(".Car").animate({
//some animation
}, oCar.Speed, function() {

});

Then it's wouldn't work since the speed would be a string.

2 Answers2

1

Have you tried?

$(".Car").animate({
//some animation
}, parseInt(oCar.Speed,10), function() {

});
Toni Michel Caubet
  • 17,157
  • 49
  • 178
  • 335
0

All HTMLInputElements will have a String for value, you could try using valueAsNumber, but I would not rely on this for now for compatibility reasons. Instead, cast the String to Number in your favourite way. For example; using a bitwise OR 0 to call JavaScript's internal ToInt32 (as described in the spec).

var oCar = {
    "Price": $('#Price').val() | 0
};
Paul S.
  • 58,277
  • 8
  • 106
  • 120