0

In JavaScript, can you change an element’s attribute value to a variable?

When changing an attribute of an HTML element, can you set the attribute value to a predefined variable in JavaScript? For example: var elem = document.getElementByTagName(“div”); desiredHeight = “100px”; elem[0].style.height = desiredHeight; I know that in this simplified example you wouldn’t need a variable if it was always going to be 100px, but mine varies in the code so i was wondering if using a string variable would work when setting and element’s attribute?

18th Jul 2018, 5:33 PM
Jimmy2890
Jimmy2890 - avatar
1 ответ
+ 1
Yes of course "100px" is a string just like var myVar = "100px";
18th Jul 2018, 5:55 PM
TurtleShell
TurtleShell - avatar