Add one value to the text box

How do I add a value to a text box? For example, the amount? This is how I get an object from the DOM - what should I do?

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

      

This is the value of my textbox :)

+2


source to share


2 answers


var elm = $(this).parent("div").children("input");
var val = elm.val();
val = Number(val) == NaN ? 0 : Number(val);
elm.val(val + 1);

      



+2


source


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



inp = $("#inputId");
var orgValue = parseInt(inp.val() == "" ? "0" : inp.val());
inp.val((orgValue == NaN ? 0 : orgValue) + 1);

      

+2


source







All Articles