Yes, it gets repetitive to use the same function over and over each time with a different argument:

var myImage = document.getElementById("myImage"); var myDiv = document.getElementById("myDiv");

So a nice thing would be a function that takes all those arguments at the same time:

function getElementsByIds(/* id1, id2, id3, ... */) { var elements = {}; for (var i = 0; i < arguments.length; i++) { elements[arguments[i]] = document.getElementById(arguments[i]); } return elements; }

Then you would have references to all your elements stored in one object:

var el = getElementsByIds("myImage", "myDiv"); el.myImage.src = "test.gif";

But you would still have to list all those ids.

You could simplify it even more if you want all elements with ids:

function getElementsWithIds() { var elements = {}; var elementList = document.querySelectorAll("[id]"); for (var i = 0; i < elementList.length; i++) { elements[elementList[i].id] = elementList[i]; } return elements; }

But it would be pretty expensive to call this function if you have many elements.

So, theoretically, if you would use the with keyword you could write code like this:

with (getElementsByIds('myButton', 'myImage', 'myTextbox')) { myButton.onclick = function() { myImage.src = myTextbox.value; }; }