0
The problem is this:
1-I created a div and defined its style by its class, in css. Including width.
2- In javascript I try to search to change the value of this width, via "myDiv.style.width", but nothing comes, it seems to be Undefined.
3-My script is written in body, after css.
4-I search for the div element in javascript via mouse function parameter over constant in it:
<div id="Barra1" class="animacao" onmouseover="increaseBar(this)"
onmouseout="decreaseBar(this)" style="background-color: royalblue;">
</div>
And my css:
<style>
.animacao {
border-bottom: 10px;
border-top: 10px;
height: 30px;
width: 10px;
}
</style>
And the javascript where I’m trying to change width, and it comes Undefined:
<script>
const initial = 10
const end = innerWidth
const step = 5
function increaseBar(elem){
const newBegining = elem.style.width+step
//Nessa console.log abaixo, só aparece o meu texto e nenhum valor do lado
console.log("Largura no css: "+elem.style.width)
if (newBegining < end){
elem.style.width = newBegining
setTimeout(() => increaseBar(elem), 1000)
}
}
</script>
NOTE: The div is coming correctly by the parameter. If I give a console.log in the "elem" parameter, the html of my div appears in the console. So that’s not the problem.
Now I have no idea what it is.
So, to get the width of the div I use elem.clientWidth or elem.offsetWidth. But to change the div, I keep assigning the new value to elem.style.width?
– Lucas Pletsch