Reputation: 185
This is a basic question, but I somehow can't find the answer.
I would like to pass a mix of string and variable as the value for an attribute, in the setAttribute() method.
This is what I have:
document.getElementById('mydiv').setAttribute("style", "width: 10%")
Instead of "width: 10%" I would like to have something like: "width: --myVar-- %"
All questions I find are related to passing only a variable but not how to mix both variable and string.
Thanx for any help!
Upvotes: 1
Views: 2839
Reputation: 1083
You shouldn't even be using setAttribute when there's a direct property and accessors:
document.getElementById('mydiv').style.width = myVar + '%';
Upvotes: 1
Reputation: 7028
You can use this way.
document.getElementById('mydiv').setAttribute("style", "width:"+ myVar+ "%")
Upvotes: 1