Yes, a function can make it easier for you:
window.onload = function() { function loadCurrency(name) { document.getElementById(name).innerHTML = toCurrency(x[i].getElementsByTagName(name)[0].firstChild.nodeValue); } loadCurrency('twentyFourK'); loadCurrency('oneOzGold'); loadCurrency('fiveOzGold'); loadCurrency('tenOzGold'); loadCurrency('oneKiloGold'); };
Also, if you have many items to download:
window.onload = function() { function loadCurrency(name) { document.getElementById(name).innerHTML = toCurrency(x[i].getElementsByTagName(name)[0].firstChild.nodeValue); } var items = ['twentyFourK', 'oneOzGold', 'fiveOzGold', 'tenOzGold', 'oneKiloGold']; items.forEach(loadCurrency); };
This requires Array.forEach , which is only available in ECMAScript 5, so thereβs a reserve here:
Array.prototype.forEach = function(action, thisArg) { for(var i = 0, l = this.length; i < l; i++) { if(i in this) { action.call(thisArg, this[i], i, this); } } };
source share