I know this:
document.getElemntById('object').style.height='50px';
But instead of '50' (string), I want to use another variable (for Example number).
I wanted to do this because I want random numbers.
Any easy way to do it?
This is how you would do it with a random number between 1 and 100. Read more about Random
var RandomNumber = Math.floor((Math.random() * 100) + 1).toString();
document.getElementById('object').style.height=RandomNumber + 'px';
W3Fools: "...so when you're ready to level up, move on." w3fools has changed.You can use a variable anywhere you can use a string literal.
var foo = "50px";
document.getElementById('object').style.height = foo;
You can generate random number like this and set the height. In the below example maxHeight is exclusive.
var minHeight = 10;
var maxHeight = 20;
var randomHeight = Math.floor((Math.random() * (maxHeight - minHeight)) + minHeight);
document.getElemntById('object').style.height = randomHeight + 'px';
Use Math.random() * 300 to get a random from 1-300 float number, then parseInt to get a integer.
int + string will get you string in JS so no need to convert, just use randompx + 'px'
var randompx = parseInt(Math.random() * 300) + 1;
//document.getElementById('object').style.height=randompx + 'px';
console.log(randompx+'px');
Try this
document.getElemntById('object').style.height= randomNumber + 'px';
+ operator will implicitly convert the LHS to a string. Calling the toString() function explicitly is pointless.
.style.height = myvar + 'px'<- it's pretty darn straight forward.Elemntshould beElement