I needed to add plus/minus buttons to an input box and then produce a message when the quantity is changed using jQuery. I've gotten the buttons to work (they change the numbers) but my alert doesn't fire. I'm stumped.
$('input#w3934_txtQantity').before("<input type='button' value='-' class='qtyminus' field='w3934_txtQantity' />");
$('input#w3934_txtQantity').after("<input type='button' value='+' class='qtyplus' field='w3934_txtQantity' />");
$(document).on( 'input', '#w3934_txtQantity', function() {
alert("Change!");
});
$(document).on( 'click', '.qtyplus', function(e) {
// Stop acting like a button
e.preventDefault();
// Get its current value
var currentVal = parseInt($('#w3934_txtQantity').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('#w3934_txtQantity').val(currentVal + 10);
} else {
// Otherwise put min quantity there
$('#w3934_txtQantity').val(0);
}
});
// This button will decrement the value till 0
$(document).on( 'click', '.qtyminus', function(e) {
// Stop acting like a button
e.preventDefault();
// Get its current value
var currentVal = parseInt($('#w3934_txtQantity').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 0) {
// Decrement one
$('#w3934_txtQantity').val(currentVal - 10);
} else {
// Otherwise put min quantity there
$('#w3934_txtQantity').val(0);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table cellspacing="0" border="0" style="width:100%;height:100%;padding-top:5px;">
<tr>
<td valign="bottom"><span id="w3934_lblQuantity" style="white-space:nowrap;">Quantity</span></td></tr>
<tr><td class="SubTotalLine"><input type="text" value="170" id="w3934_txtQantity" style="text-align:right;" /></td></tr>
</table>