javascript - Set an attribute for an element -


i wrote function creating new elements. code works correctly, function doesn't set attribute element(my browser don't threw error). tried several methods nothing worked.

function createelement(name, element, attribute, valueofattribute, text, indexofchildnodes) {     name = document.createelement(element.tolowercase());     var nodetext = document.createtextnode(text);     name.appendchild(nodetext);     var l = indexofchildnodes;     document.childnodes[l].appendchild(name);      if(typeof attribute === 'array' && typeof valueofattribute === 'array'){         for(var = 0, len = attribute.length; i<len; i++){             //name.setattribute(attribute[i], valueofattribute[i]);             var attr = document.createattribute(attribute[i]);             attr.value = valueofattribute[i];             name.setattributenode(attr);         }     } else {         return 'check "attribute" , "valueofattribute" arguments';     } }  createelement('next', 'button', ['id'], ['next'], 'next', 1); 

it happens because typeof arrays never return "array" in javascript.

you may either use modern array.isarray() method instead or try old-school trick prototype:

if (object.prototype.tostring.call(attribute) === '[object array]') { ... } 

demo: http://jsfiddle.net/qxrwz/


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -