I am using an html form that submits data to a MYSQL database. I need to add a button that will increase the number in the text box by one every time pressed. My code looks like this:
<label for="htop">Top: </label>
<input type="button" name="decrease" value="-" /><input type="text" name="htop" value="0" />
<input type="button" name="increase" value="+" />
What is the best way to do this?
I am using an html form that submits data to a MYSQL database. I need to add a button that will increase the number in the text box by one every time pressed. My code looks like this:
<label for="htop">Top: </label>
<input type="button" name="decrease" value="-" /><input type="text" name="htop" value="0" />
<input type="button" name="increase" value="+" />
What is the best way to do this?
Share Improve this question asked Mar 15, 2012 at 13:49 AquaMorphAquaMorph 5073 gold badges7 silver badges14 bronze badges 1- 2 Have you written any javascript yet? Post that too. – Vincent Ramdhanie Commented Mar 15, 2012 at 13:51
6 Answers
Reset to default 1put the script tag in your head element
<script>
function increaseBtnOnclick() {
document.getElementById("htop").value = Number(document.getElementById("htop").value) + 1;
}
</script>
<label for="htop">Top: </label>
<input type="button" name="decrease" value="-" /><input type="text" name="htop" value="0" id="htop"/>
<input type="button" name="increase" value="+" onclick="increaseBtnOnclick()"/>
Start with:
<input type="number">
Then add a shim if you want to have support in browsers that don't support that part of HTML 5 yet.
maybe something like this using jQuery...
$(document).ready( function() {
var elm = $('#htop');
function spin( vl ) {
elm.val( parseInt( elm.val(), 10 ) + vl );
}
$('#increase').click( function() { spin( 1 ); } );
$('#decrease').click( function() { spin( -1 ); } );
});
with
<label for="htop">Top: </label>
<input type="button" id="decrease" value="-" /><input type="text" id="htop" value="0" />
<input type="button" id="increase" value="+" />
HTH,
--hennson
You would use one read only text input with the number, and javascript for in- and decreasing the value of the input field via 2 buttons. When the desired value is reached, the user would press a submit button for the form to be sent and saved into the database.
Using Javascript add a 'click' event to the + button:-
<input type="button" name="increase" value="+" onclick='document.getElementById("htop").value = document.getElementById("htop").value + 1"' />
This will increase the value in the field and when the form is submitted, the relevant value is returned to the server. The '-' button needs the same but decreasing the value. You may also wish to add a check that the value never goes below 0 or above an upper limit too.
Using jQuery, something like this would work.
$("button[name=decrease]").click(function() {
$("input[name=htop]").val(parseInt($("input[name=htop]").val()) - 1);
});
$("button[name=increase]").click(function() {
$("input[name=htop]").val(parseInt($("input[name=htop]").val()) + 1);
});