Contoh Set Atribut Elemen HTML

<html>
<head>
   <title>Belajar setAttribute()</title>
</head>
<body>

<div id="div_ku">
Ini Div Ku
</div>

<hr />
<form name="fr" onsubmit="return false">
Nama atribut: <input type="text" name="nama" value="align" /><br />
Isi Atribut : <input type="text" name="isi" value="center" /><br />
<button onclick="terapkanAtribut()">Terapkan Atribut</button>
</form>
<script type="text/javascript">

var elemen = document.getElementById('div_ku');

function terapkanAtribut(){
    elemen.setAttribute(document.fr.nama.value,document.fr.isi.value);
}
</script>
</body>
</html>

Sumber : http://www.developer-id.com/