html - How can I execute a javascript function when an input changes? -
i trying execute javascript function when input changes value. input changes value when other inputs clicked (those inputs increase or decrease value).
here html code:
<input type='button' value='–' class='qtyminus' field='quantity{{$accessory- >id}}' style="font-weight: bold;" /> <input id='accessory{{$i}}' onchange="updatenoaccessories({{$i}})" type='text' name='quantity{{$accessory->id}}' value='1' class='qty' style="margin-bottom: 0px !important"/> <input type='button' value='+' class='qtyplus' field='quantity{{$accessory- >id}}' style="font-weight: bold;" />
js function:
function updatenoaccessories(i) { var no = document.getelementbyid("accessory"+i).value; document.getelementbyid("acc"+i).innerhtml = no + ' x'; }
what want update js function:
<span>you chose:<i><b id="acc0"></b> items,</i><i><b id="acc1"></b> things,</i><i><b id="acc2"></b>stuff</i></span>
your button + , - not linked input onchange event never triggerd (maybe didn't paste part of code) , can use oninput instead of onchange.
you can fix :
<input type='button' value='–' class='qtyminus' field='quantity{{$accessory- >id}}' style="font-weight: bold;" onclick='updatevalue(-1,$i)'/> <input id='accessory{{$i}}' oninput="updatenoaccessories({{$i}})" type='text' name='quantity{{$accessory->id}}' value='1' class='qty' style="margin-bottom: 0px !important"/> <input type='button' value='+' class='qtyplus' field='quantity{{$accessory- >id}}' style="font-weight: bold;" onclick='updatevalue(+1,$i)'/>
and js :
function updatevalue(i,id) { var no = document.getelementbyid("accessory"+id).value; no = no + i; document.getelementbyid("acc"+id).innerhtml = no; } function updatenoaccessories(i) { var no = document.getelementbyid("accessory"+i).value; document.getelementbyid("acc"+i).innerhtml = no + ' x'; }
Comments
Post a Comment