Capítulo 14

DOM Core setAttributeNode

JavaScript

<script type="text/javascript">
window.onload = function () {
var elBotao, container, atributo;	
	elBotao = document.getElementById("btn");	
	container = document.getElementById("container");
	atributo = document.createAttribute("class");
	atributo.value = "um";
	
	elBotao.onclick = function() {
	container.setAttributeNode(atributo);
	}
}
<script>

HTML

...
<div id="container">Container</div>
<button type="button" id="btn">Rodar Script</button>
...

Container