Change multiple properties of a DOM element with javascript -


i have simple function called loadstyles() here is:

function loadstyles(url) {         var link = document.createelement('link');         link.rel = 'stylesheet';         link.type = 'text/css';         link.href = url;         link.media = 'all';         (document.getelementsbytagname("head")[0] || document.documentelement).appendchild(link); } 

i wondering if there's way of adding values of properties except one: link.rel equals ..., link.type equals ..., etc.

i tried this:

function loadstyles(url) {     var link = document.createelement('link');     link += {         rel: 'stylesheet',         type: 'text/css',         href: url,         media: 'all'     };     (document.getelementsbytagname("head")[0] || document.documentelement).appendchild(link); } 

but doesn't work. know it's stupid try (because ide says {....} expression not assignable type html element ^^').

i think there nothing in javascript standard.

but use function that

function setattributes(element, attributes) {   (var name in attributes) {     element.setattributes(name, attributes[name]);   } }  function loadstyles(url) {     var link = document.createelement('link');     setattributes(link, {         rel: 'stylesheet',         type: 'text/css',         href: url,         media: 'all'     });     (document.getelementsbytagname("head")[0] || document.documentelement).appendchild(link); } 

i didn't test it, should work.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -