Javascript: Getting undefined on input box that takes a range from 0 to 10 -
i have 2 buttons (+ , -) , input box value of 10. input must take value 0 10 , can't figure out why i'm getting undefined after reach either 0 or 10 on input box. tried using parseint values. perhaps i'm not doing on right place. appreciate help.
var fighterscore1 = document.getelementbyid("fighterscore1"); fighterscore1.value = 10; var fighter1inc = document.getelementbyid("fighter1inc"); var valor = 10; fighter1inc.onclick = function (valor) { fighterscore1.value = increasevalue(valor); }; fighter1dec.onclick = function (valor) { fighterscore1.value = decreasevalue(valor.value); }; function decreasevalue() { if (valor <= 0 ) { } else { valor--; return valor; } }; function increasevalue() { if (valor >= 10 ) { valor = 10; } else { valor++; valor = valor; return valor; } };
<div id="rwid1"> <button id="fighter1inc">+</button> <input type="text" id="fighterscore1" maxlength="10" onkeypress='return event.charcode >= 48 && event.charcode <= 57'> <button id="fighter1dec">-</button> </div><!--/rwid-->
the problem you're returning inside condition less 10 or greater 0. if don't return in 1 condition,there no return value end assigning undefined
variable. here fixed jsfiddle. have return outside if statements, , valor.value
doesn't exist.
var fighterscore1 = document.getelementbyid("fighterscore1"); fighterscore1.value = 10; var fighter1inc = document.getelementbyid("fighter1inc"); var valor = 10; fighter1inc.onclick = function (valor) { fighterscore1.value = increasevalue(); }; fighter1dec.onclick = function (valor) { fighterscore1.value = decreasevalue(); }; function decreasevalue() { if (valor <= 0 ) { valor = 0; } else { valor--; } return valor; }; function increasevalue() { if (valor >= 10 ) { valor = 10; } else { valor++; } return valor; };
<div id="rwid1"> <button id="fighter1inc">+</button> <input type="text" id="fighterscore1" maxlength="10" onkeypress='return event.charcode >= 48 && event.charcode <= 57'> <button id="fighter1dec">-</button> </div><!--/rwid-->
a note: pass arguments function takes none (js accepts varying number of arguments, if don't specify). either remove call arguments, or use argument , pass in, using argument in place of valor
.
Comments
Post a Comment