Reputation: 1601
input val need to change on click
This is my code
<div class="inputQty">
<span>
<small class="up">^</small>
<small class="down">V</small>
</span>
<input type="text" maxlength="6" name="oa_quantity" class="input-quantity" value="1"/>
</div>
if we click on ".up" input val change to 1++, if we click on ".down" input val change to 1- -
Please help me
ja fiddle here http://jsfiddle.net/X57Mz/
Upvotes: 0
Views: 2334
Reputation: 15190
It can be done like this. Check working sample of jsfiddle http://jsfiddle.net/X57Mz/4/
$(document).ready(function() {
$(".up").click(function() {
$(".input-quantity").val(parseInt($(".input-quantity").val())+1);
});
$(".down").click(function() {
$(".input-quantity").val(parseInt($(".input-quantity").val())-1);
});
});
If you don't want to go to minus number and want non-negative box, you can do it like this
$(document).ready(function() {
$(".up").click(function() {
$(".input-quantity").val(parseInt($(".input-quantity").val())+1);
});
$(".down").click(function() {
if(parseInt($(".input-quantity").val())>0)
$(".input-quantity").val(parseInt($(".input-quantity").val())-1);
});
});
Here is jsfiddle http://jsfiddle.net/X57Mz/6/
Upvotes: 2
Reputation:
demo http://jsfiddle.net/7hzcB/6/
Jquery code
<script type="text/javascript">
$(function() {
$(".up").click(function() {
$(".input-quantity").val(parseInt($(".input-quantity").val()) + 1)
});
$(".down").click(function() {
$(".input-quantity").val(parseInt($(".input-quantity").val()) - 1)
});
});
</script>
Html
<div class="inputQty">
<span>
<small class="up">^</small>
<small class="down">V</small>
</span>
<input type="text" maxlength="6" name="oa_quantity" class="input-quantity" value="1"/>
</div>
Upvotes: 2
Reputation: 87073
$('.inputQty small').on('click', function() {
var cls = $(this).hasClass('up'); // check that it has up class
$('.input-quantity').val(function() {
// if up class exists then increase value by 1 else decrease by 1
return cls ? parseInt(this.value) + 1 : parseInt(this.value) - 1;
});
});
Upvotes: 0
Reputation: 1094
Here Goes Your code ..
$('.up').click(function()
{
var temp=$('.input-quantity').val();
$('.input-quantity').val(parseInt(temp)+parseInt(1));
});
$('.down').click(function()
{
var temp=$('.input-quantity').val();
$('.input-quantity').val(parseInt(temp)-parseInt(1));
});
Upvotes: 2
Reputation: 146
$(document).ready( function() {
var el = $('#test');
function change( amt ) {
el.val( parseInt( el.val(), 10 ) + amt );
}
$('.up').click( function() {
change( 1 );
} );
$('.down').click( function() {
change( -1 );
} );
} );
Upvotes: 0
Reputation: 34107
Yet another demo http://jsfiddle.net/3v3YL/2/
code B-)
var inputval = parseInt($('.input-quantity').val());
$(".up").click(function(){
$('.input-quantity').val(inputval++);
});
$(".down").click(function(){
$('.input-quantity').val(inputval--);
});
Upvotes: 0
Reputation: 42440
Grab the current value, increment it by 1, replace the value:
$(document).ready(function(){
$('.up').on('click',function(){
var value = parseInt($('.input-quantity').first().val());
value++;
$('.input-quantity').first().val(value);
});
});
Getting the down button working is a trivial modification of the above code.
Upvotes: 0
Reputation: 13461
Use this:
$(function(){
$('.up').on('click',function(){
$('.input-quantity').val(parseInt($('.input-quantity').val())+1);
});
$('.down').on('click',function(){
$('.input-quantity').val(parseInt($('.input-quantity').val())-1);
});
});
Upvotes: 2