title: JavaScript - create element and set attributes
body: <p>I've got these functions to create elements and change their attributes. Could you give me an advice on how to modify them?</p>
  
  <pre><code>function create(elem) {
  return document.createElementNS ? document.createElementNS("http://www.w3.org/1999/    xhtml", elem) : document.createElement(elem);
  }
  
  function attr(elem, name, value) {
   if (!name || name.constructor != String) return "";
   name = {"for": "htmlFor", "class": "className"}[name] || name;
   if (typeof value != "undefined") {
    elem[name] = value;
    if (elem.setAttribute) elem.setAttribute(name, value);
   }
   return elem[name] || elem.getAttribute(name) || "";
  }
  </code></pre>
  
  <p>I want to get something like this <strong>create('div', {'id': 'test', 'class': 'smth'});</strong></p>
  
  <pre><code>function create(elem, attr) {
   if (!attr) return document.createElementNS ? document.createElementNS("http://www.w3.org/1999/xhtml", elem) : document.createElement(elem);
   if (attr) {
    var el = document.createElementNS ? document.createElementNS("http://www.w3.org/1999/xhtml", elem) : document.createElement(elem);
    for (var i = 0; i &lt; attr.length; i++) {
     attr(el, name[i], value[i]);
    }
    return el;
   }
  </code></pre>
  
  <p>}</p>
  
  <p>Please help =]</p>