Q:

Trying to change attribute of multiple items in JavaScript, only first item is changing, what am I doing wrong?

<div class="t">Hello</div>
<div class="t">Hello</div>

<script>
    function $$(name) {
        a = document.getElementsByTagName("*");
        for (var i = 0; i < a.length; i++) {
            if (a[i].className == name) return a[i];
        }
    }

    $$('t').style.color = "red";
</script>

Only the first div with a class of 't' turned red. What did I do wrong?

A:

You're exiting the loop as soon as a match is found, so by definition only one element will ever get changed.
Oh, and your code will fail if the element you want to match has extra classes on it too.
It would be better to write your code to invoke a callback on all matching elements:
function $$(name, cb) {
    var a = document.getElementsByTagName("*");
    for (var i = 0; i < a.length; i++) {
        if (a[i].className == name) {
            cb.apply(a[i])
        }
    }
}

$$('t', function() {
    this.style.color = 'red';
});

Or, just use a library (e.g. jQuery) that's designed for exactly this sort of job:
$('.t').css('color', 'red');

