"Jquery increment and decrement"
Bootstrap 4.1.1 Snippet by imsachin

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!------ Include the above in your HEAD tag ----------> <!DOCTYPE html> <html lang="en"> <head> <title>HTML</title> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700" rel="stylesheet"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!--[if IE]> <link href="~/Content/NewHomePage/all-ie-only.css" rel="stylesheet" /> <![endif]--> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <link rel="stylesheet" href="style.css"> </head> <body> <form id='myform' method='POST' action='#'> <input type='button' value='-' class='qtyminus' field='quantity' /> <input type='text' name='quantity' value='0' class='qty' /> <input type='button' value='+' class='qtyplus' field='quantity' /> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="custom.js"></script> </body> </html>
#myform { text-align: center; padding: 5px; border: 1px dotted #ccc; margin: 2%; } .qty { width: 40px; height: 25px; text-align: center; } input.qtyplus { width:25px; height:25px;} input.qtyminus { width:25px; height:25px;}
jQuery(document).ready(function(){ // This button will increment the value $('.qtyplus').click(function(e){ // Stop acting like a button e.preventDefault(); // Get the field name fieldName = $(this).attr('field'); // Get its current value var currentVal = parseInt($('input[name='+fieldName+']').val()); // If is not undefined if (!isNaN(currentVal)) { // Increment $('input[name='+fieldName+']').val(currentVal + 1); } else { // Otherwise put a 0 there $('input[name='+fieldName+']').val(0); } }); // This button will decrement the value till 0 $(".qtyminus").click(function(e) { // Stop acting like a button e.preventDefault(); // Get the field name fieldName = $(this).attr('field'); // Get its current value var currentVal = parseInt($('input[name='+fieldName+']').val()); // If it isn't undefined or its greater than 0 if (!isNaN(currentVal) && currentVal > 0) { // Decrement one $('input[name='+fieldName+']').val(currentVal - 1); } else { // Otherwise put a 0 there $('input[name='+fieldName+']').val(0); } }); });

Related: See More


Questions / Comments: