title: Add CSS attributes to element with JavaScript
body: <p>I want to add CSS attributes to my element, but my current solution loses all previous attributes that had an impact on the element.</p>
  <pre class="lang-javascript prettyprint-override"><code>function checkNr(id) {
    var value = document.getElementById(id).value;
    if (parseFloat(value) == NaN) {
      document.getElementById(id).setAttribute(&quot;style&quot;, &quot;border:2px solid red; background-color: rgb(255, 125, 115);&quot;);
    }
    else {
      document.getElementById(id).setAttribute(&quot;style&quot;, &quot;border:default; background-color: rgb(255, 255, 255);&quot;);
    }
  }
  </code></pre>
  <p>Before using this method the element already had the attributes:</p>
  <pre><code>float: left;
  width: 50px;
  </code></pre>
  <p>Afterwards, the element loses these attributes, leaving only the specific attributes from the JavaScript method. So, I want to add attributes without replacing them.</p>