title: add id to dynamically created <div>
body: <p>I have the following JavaScript that creates a div and then appends it to the body and then inserts some dynamically generated HTML into it. cartDiv = document.createElement('div'); This div I would like to add an id and/or a class to it. If possible both Jquery and JavaScript answers would be great.</p>
  
  <pre><code>var cartHTML = '&lt;div class="soft_add_wrapper" onmouseover="setTimer();"&gt;';
  cartHTML += '&lt;div class="soft_add_header_shadow"&gt;';
  cartHTML += '&lt;div class="soft_add_header"&gt;&lt;span class="soft_add_span"&gt;Added to cart&lt;/span&gt;&lt;a href="" class="close_btn" onclick="hideCart(); return false;"&gt;Close&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;'
  cartHTML += '&lt;div class="soft_add_content_shadow"&gt;&lt;div class="soft_add_content_wrapper"&gt;';
  cartHTML += '&lt;div class="soft_add_content_area" onscroll="setTimer();"&gt;&lt;table class="cart_table" cellpadding="0" cellspacing="0" border="0"&gt;';
  if (cartLength != 0) {
      cartHTML += cartLoop(index, cartLength);
      if (index != 0) {
          cartHTML += cartLoop(0, index);
      }
      if (discountTotal != "0") {
          var discountProduct = {
          ProductName: "Discount(s)",
          ProductPrice: '&lt;span style="color:red"&gt;' + discountTotal + '&lt;/span&gt;'
          }
          cartHTML += getLineItemHTML(discountProduct);
      }
  }
  cartHTML += '&lt;/table&gt;&lt;/div&gt;&lt;div class="soft_add_sub_total"&gt;&lt;div class="number_of_items"&gt;' + quantity + ' items in cart&lt;/div&gt;';
  cartHTML += '&lt;div class="sub_total"&gt;';
  cartHTML += 'Subtotal: &lt;span class="sub_total_amount"&gt;' + cartTotal + '&lt;/span&gt;';
  cartHTML += '&lt;/div&gt;';
   cartHTML += '&lt;/div&gt;&lt;div class="soft_add_action_area"&gt;&lt;a href="/ShoppingCart.asp" class="check_out"&gt;Check Out&lt;/a&gt;';
  cartHTML += '&lt;a href="" class="continue_shopping" onclick="hideCart(); return false;"&gt;Continue shopping&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';
  if (cartDiv == null) {
      cartDiv = document.createElement('div');
      document.body.appendChild(cartDiv);
  }
  cartDiv.innerHTML = cartHTML;
  </code></pre>