-2
I have an extra button on the screen that increments +1 on top of the current value of the input, when I make an Alert in the variable presents the normal value incrementing +1 but in the field on screen the value does not change, always zero, what am I doing wrong? Can anyone help me? Follow the code:
<div class="col-md-12 col-sm-12 col-xs-12" style="margin:0;padding:0;">
<div class="col-md-6 col-sm-6 col-xs-6" style="margin:0;padding:0;float:left;width:60%;">
<h6 style="float:left;"><?echo utf8_encode($rowAdicionaisProduct['ingredient']);?></h6>
<small style="float:left;clear:both;color:#ED3237;padding:0px;line-height:10px;"><b>+ R$<?=$rowAdicionaisProduct['unit_price']?></b></small>
</div>
<input type="hidden" name="idIngredientAdds[]" value="<?=$idIngredientAdicional?>">
<input type="hidden" name="ingredient[]" value="<?echo utf8_encode($rowAdicionaisProduct['ingredient']);?>">
<input type="hidden" name="priceAdd[]" id="adicionalPrice<?=$idIngredientAdicional?>" value="<?=$adicionalPrice?>">
<button type="button" class="btnChangeQtdAdd" style="float:left;background:transparent;border:none;margin-top:-15px;" id="decAdd<?=$idIngredientAdicional?>" onclick ="subtrairMenos1Add<?=$idIngredientAdicional?>();"><i style="font-size:19px;color:#ED3237;" class="fa fa-minus"></i></button>
<input type="text" name="qtd_option[]" class="qtdAdicionais" id="qtdIngredientAdicional<?=$idIngredientAdicional?>" style="width:30px;float:left;height:30px;text-align:center;margin-top:-20px;" value="0">
<button type="button" class="btnChangeQtdAdd" style="float:left;background:transparent;border:none;margin-top:-15px;" id="inc<?=$idIngredientAdicional?>" onclick="somarMais1Add<?=$idIngredientAdicional?>();"><i style="font-size:19px;color:#ED3237;" class="fa fa-plus"></i></button>
</div>
<script>
function somarMais1Add<?=$idIngredientAdicional?>() {
var n1 = document.getElementById("qtdIngredientAdicional<?=$idIngredientAdicional?>").val;
var n2 = 1;
document.getElementById("qtdIngredientAdicional<?=$idIngredientAdicional?>").val = n1 + 1;
};
</script>
Are you creating a function for each button? It’s a bad way to do this, let me tell you. Just one function and send the id as a parameter:
function somarMais1Add(id)
, and in HTML:onclick="somarMais1Add('<?=$idIngredientAdicional?>');"
– Sam
I only know how to do it... I can’t do it any other way
– William De Paula