I am trying to turn rectangle.style.width = "100px";
into a variable using parseInt so that I can place that variable into two buttons so I can either increase the width of my rectangle or decrease it. However I cant seem to get the information into the variable no matter what I do.
My two functions are very similar with the only diffrence being that one subtracts and the other adds.
//does not work as supposed to
function shrinkBtn() {
myWidth= myWidth - 10 + "px";
//works but does not have the element as a variable
function shrinkBtn() {
rectangle.style.width = parseInt(rectangle.style.width)- 10+ "px";
}
I would have thought that this would have been super simple but I havent had any luck with any of answers I have found from previous posts or google searches.
I have tried a couple diffrent things when facing this issue. I have tried
var myWidth = rectangle.style.width;
var myWidth = parseInt(rectangle.style.width);
I do get a output when I place it in a console log such as console.log(myWidth) however it doesnt seem to work when I try to put it in my other functions. I was expecting to be able to store the information into the variable and then place it in my function such as
function shrinkBtn() {
myWidth = parseInt(myWidth) -10 + "px";
}
or
function shrinkBtn() {
myWidth = myWidth - 10 + "px";
}
EDIT: added full code
rectangle.style.width = "100px";
// rectangle.style.width = parseInt(rectangle.style.width)+ 10+ "px";
var myWidth = parseInt(rectangle.style.width);
console.log(myWidth);
decreaseBtn.addEventListener("click", shrinkBtn);
increaseBtn.addEventListener("click", growBtn);
function shrinkBtn() {
//using the variable here does not trigger the function
myWidth = parseInt(myWidth) -10 + "px";
//using this style below does trigger the function
// rectangle.style.width = parseInt(rectangle.style.width)- 10+ "px";
}
function growBtn() {
//Original function without the variable to show comparison This works
rectangle.style.width = parseInt(rectangle.style.width)+ 10+ "px";
}
The OP might have a look into a function's bind
method.
Thus as for the next provided example one does not register handler functions which each do query the specific element each time the handler gets invoked, but one does create and register two handler functions where the element is bound as the created function's this
context (the OP should think in terms of two methods which do operate each on the bound object).
function decreaseWidthOfBoundElement() {
this.style.width = parseInt(this.style.width, 10) - 10 + "px";
}
function increaseWidthOfBoundElement() {
this.style.width = parseInt(this.style.width, 10) + 10 + "px";
}
function main() {
const elmNode = document.querySelector('#rectangle');
document
.querySelector('#shrink')
.addEventListener(
'click', decreaseWidthOfBoundElement.bind(elmNode)
);
document
.querySelector('#increase')
.addEventListener(
'click', increaseWidthOfBoundElement.bind(elmNode)
);
}
main();
<div id="rectangle" style="width: 100px; height: 100px; background-color: red;"></div>
<button id="shrink">-</button>
<button id="increase">+</button>
In addition one can bind arguments too which provides even more options for the customization of changing an element's size (e.g. implementation of just a single function which then gets used for binding).
function changeSizeOfBoundElement(deltaX = 0, deltaY = 0) {
this.style.width = parseInt(this.style.width, 10) + deltaX + "px";
this.style.height = parseInt(this.style.height, 10) + deltaY + "px";
}
function enableElementChange(changeRoot) {
const changeNode = changeRoot
.querySelector('[data-element]');
const decreaseNode = changeRoot
.querySelector('[data-decrease-values]');
const increaseNode = changeRoot
.querySelector('[data-increase-values]');
const decValues = decreaseNode.dataset.decreaseValues
.split(',').map(value => Number(value.trim()));
const incValues = increaseNode.dataset.increaseValues
.split(',').map(value => Number(value.trim()));
decreaseNode
.addEventListener(
'click', changeSizeOfBoundElement
.bind(changeNode, ...decValues)
);
increaseNode
.addEventListener(
'click', changeSizeOfBoundElement
.bind(changeNode, ...incValues)
);
}
document
.querySelectorAll('[data-element-change]')
.forEach(enableElementChange);
[data-element-change] { margin-bottom: 20px; }
<div data-element-change>
<div data-element style="width: 70px; height: 70px; background-color: orange;"></div>
<!-- data-decrease-values="<deltaX>,<deltaY>" -->
<button data-decrease-values="-10">-</button>
<button data-increase-values="10">+</button>
</div>
<div data-element-change>
<div data-element style="width: 200px; height: 50px; background-color: purple;"></div>
<!-- data-decrease-values="<deltaX>,<deltaY>" -->
<button data-decrease-values="-20,-5">-</button>
<button data-increase-values="20,5">+</button>
</div>