/ Published in: JavaScript
Released into the public domain.
Expand |
Embed | Plain Text
function changeField(fieldToChange, incVal, boundValues, validUnits) { // boundValues indices: 0 is max, 1 is min, 2 is "max set?", 3 is "min set?" for(var i=0;i<2;i++) { if(typeof boundValues[i]=='undefined'||(typeof boundValues[i]!='undefined'&&isNaN(boundValues[i]))) boundValues[i] = Number.POSITIVE_INFINITY * (i==1?-1:1); else boundValues[i+2] = true; } newValue = Math.round((parseFloat(fieldToChange.value) + (incVal ? 1 : -1))*100)/100; if(newValue > boundValues[0] || newValue < boundValues[1]) // check if out of bounds newValue = incVal ? (boundValues[3] ? boundValues[1] : 0) : (boundValues[2] ? boundValues[0] : 0); unitsUsed = fieldToChange.value.replace(/[0-9\-\.]/g, ''); fieldToChange.value = (isNaN(newValue) || find_index(validUnits, unitsUsed) == -1 ? '0' : newValue) + unitsUsed; return fieldToChange.value; }
You need to login to post a comment.
