Mick Mick - 3 months ago 10
ASP.NET (C#) Question

press button and value increases in text box

So when the page loads the text box will contain a stored value. I want the user to press the '+' button and the value in the text box will increase by one. Im guessing this is done with JQuery...Any ideas on where to get started so far I have...

<input type="text" name="BoqTextBox" id="BoqTextBox" value="0" />
<input type="Button" value="+" onclick="AddOne(document.getElementById('BoqTextBox').value)" />

<script>
function Add(data) {
//so the current digit is passed to here, where I need to do some funky code
//where it increments the current digit by one and stores it in BoqTextBox - replacing the old digit.

//Also to note if the text box contains 124.54 for example and + is pressed
//then new value will be 125.54
}
</script>


Any assistance with this would be great.

Thank you

...something like data = data + 1, but then how do I return the value into the text box?

Answer

You can use jQuery's val() to fetch and set a value. In this case the code you need could look like this (demo):

<input type="text" name="BoqTextBox" id="BoqTextBox" value="0" />
<input type="Button" id='AddButton' value="+" />
<script>
$('#AddButton').on('click', function () {
    var input = $('#BoqTextBox');
    input.val(parseFloat(input.val()) + 1);
})
</script>
Comments