tags:

views:

141

answers:

2

How do i add one to the value of a text field? For example, quantity? This is how i get the object from the DOM - what do i do from here?

$(this).parent("div").children("input").val();

This is the value of my text field :)

+2  A: 

Using jQuery, also handling the empty condition when there is no value present

inp = $("#inputId");
var orgValue = parseInt(inp.val() == "" ? "0" : inp.val());
inp.val((orgValue == NaN ? 0 : orgValue) + 1);
thr
parseInt will return NaN if the value is non numeric. NaN+1 = NaN.
Marius
should work for NaN now
thr
ussing parseInt you assume that the field holds an integer. It might hold a decimal number, in which case the Number() function is better
Marius
+1  A: 
var elm = $(this).parent("div").children("input");
var val = elm.val();
val = Number(val) == NaN ? 0 : Number(val);
elm.val(val + 1);
Marius
Will give weird results on non-number-strings.
thr
This is kind of what i want, but i dont want to append the number 1 - i want to add 1 onto it? Is there a way to do this... 10 > 11 after running my code
tarnfeld
Try with the updated code.
Marius
THANKS!! :D IT WORKS :D WOOO
tarnfeld