/* NEED TO SHIM IT? */
if(typeof window.Element == 'undefined')
{
window["Element"] = (function()
{
var obj = {};
/* add other things to Elements here */
obj.prototype = {};
return obj;
})();
var __getElementById = document.getElementById;
var __createElement = document.createElement;
document.getElementById = function(id)
{
var element = __getElementById(id);
for(var prop in Element)
{
element[prop] = Element[prop];
}
for(var proto in Element.prototype)
{
element[proto] = Element.prototype[proto];
}
}
document.createElement = function(tag)
{
var element = __createElement(tag);
}
}
/* END of shim */
/* the actual method */
Element.prototype.setAttributes = function(attr)
{
var recursiveSet = function(at,set)
{
for(var prop in at)
{
/* check if object and not a dom node or array */
if(typeof at[prop] == 'object' && at[prop].dataset == null && at[prop][0] == null)
{
recursiveSet(at[prop],set[prop]);
}
else
{
set[prop] = at[prop];
}
}
}
recursiveSet(attr,this);
}
var test = document.getElementById("test");
/* test use */
test.setAttributes({
style:
{
background:"#fff",
color:"#000",
width:"100px"
},
className:"testclass",
onclick: function(e){alert("the test div has been clicked and its width is: "+this.style.width);}
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.