i wanted to turn this strings in this array to variables so i could assign them to html elements:
let array = ['varOne', 'varTwo', 'varThree']
let array[0] = document.getElementById('id')
i wanted to turn this strings in this array to variables so i could assign them to html elements:
let array = ['varOne', 'varTwo', 'varThree']
let array[0] = document.getElementById('id')
You almost certainly don't want to do this, but if you did here is how:
const names = ['varOne', 'varTwo', 'varThree']
names.forEach((variableName) => {
window[variableName] = document.getElementById(variableName)
})
console.log(varOne)
console.log(varTwo)
console.log(varThree)
<html>
<body>
<div id='varOne' />
<div id='varTwo' />
<div id='varThree' />
</body>
</html>