You can define a function that do like:
function setElementAttrbute(tagname,index,attrName,value){
var elements = document.getElementsByTagName(tagname); // returns kind of array
elements[index].setAttribute(attrName,value);
}
When you would like to use array-methods like (slice,push,pop,shift,unshift), you can cast it by doing this:
document.getElementsByTagName(tagname); // returns kind of array that does not have got array.-methods like above.
elements = Array.prototype.slice.call(elements); // returns real array