I have a form with many input=text and I have neraby two image (-,+) I want to increase or decrease the previous input text. I don’t want to put into the input text an id because I can have a lot of this line with input text and minus or plus.
With my code the minus button works but the plus button not why?
My jQuery code
$('.minus').click(function(){
var intRegex = /^\d+$/;
var floatRegex = /^((\d+(\.\d *)?)|((\d*\.)?\d+))$/;
var num = $(this).prev('input').val();
if(intRegex.test(num) || floatRegex.test(num)) {
if (num>0){
$(this).prev('input').val(num-1);
}
}
else{
//non è un numero
}
});
$('.plus').click(function(){
var intRegex = /^\d+$/;
var floatRegex = /^((\d+(\.\d *)?)|((\d*\.)?\d+))$/;
var num = $(this).prev.prev('input').val();
if(intRegex.test(num) || floatRegex.test(num)) {
$(this).prev.prev('input').val(num+1);
}
else{
//non è un numero
}
});
HTML:
<input type="text" name="qta" id="qta" style="width:50px;"/>
<img src="img/shop/meno.jpg" alt="" class="minus" style="cursor:pointer"/>
<img src="img/shop/piu.png" alt="" class="plus" style="cursor:pointer"/>
and
You missed a
()twiceanyway I would wrap every input and images in their own wrapper and I would find the input with
$(this).parent().find('input')