West Coast Charlie West Coast Charlie - 1 year ago 99
HTML Question

How to use Javascript to add multiple style properties to one element?

I've tried various renditions of this code to try and change a certain element for a coding exercise but non of them seems to be able to change multiple styling properties of an element on a button click. Would love some assistance. Thanks!

document.getElementById("Combo Style").onclick = function() {
document.getElementById ("More Text").style.fontSize.color = "50px , #BB65C5";

Answer Source

To achieve your expected result use setAttribute

<button id="Combo Style">Change</button>
<div id="More Text">abcd</div>


document.getElementById("Combo Style").onclick = function() {
  document.getElementById("More Text").setAttribute("style", "font-size:50px;color:red;");



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download