I'm not able to store the style of an element in a variable and use it as the route:
let store = element.style.height;
store = 100px;
I'm trying to reduce my code by creating a constant that limits the max value of an input like follows:
const limitMaxVal = (whatToLimit, whatToStyle) => {
let maxVal = parseFloat(whatToLimit.max);
let minVal = parseFloat(whatToLimit.min);
if (whatToLimit.value >= maxVal) {
whatToLimit.value = maxVal;
whatToStyle = maxVal / 10 + "%";
} else if (whatToLimit.value <= minVal) {
whatToStyle = minVal / 10 + "%";
} else {
whatToStyle = whatToLimit.value / 10 + "%";
}
}
The constant has two variable. whatToLimit is the input I'm limiting and whatToStyle is what the input is styling. In this case I'm styling the height of a canvas but I want to use the same constant for the width, background-color... Which brings me to my problem: I can't find a way to store the style of an element as a route. If I store it like it follows I'll get the value of the height:
inputHeight.oninput = function() {
limitMaxVal(this, canvas.style.height);
};
Another example of using the same constant would be:
inputWidth.oninput = function() {
limitMaxVal(this, canvas.style.width);
};
I also tried using eval() but it doesn't work either:
const limitMaxVal = (whatToLimit, whatToStyle) => {
let maxVal = parseFloat(whatToLimit.max);
let minVal = parseFloat(whatToLimit.min);
if (whatToLimit.value >= maxVal) {
whatToLimit.value = maxVal;
eval(whatToStyle) = maxVal / 10 + "%";
} else if (whatToLimit.value <= minVal) {
eval(whatToStyle) = minVal / 10 + "%";
} else {
eval(whatToStyle) = whatToLimit.value / 10 + "%";
}
this.focus();
}
inputHeight.oninput = function() {
let store = "canvas.style.height";
limitMaxVal(this, store);
};