In compliant browsers you could simply use document.querySelector()
:
var x = document.querySelector('.test > a');
x.innerHTML = 'Hello world';
document.querySelector
returns a single element (the only element, or the first of multiple elements), rather than a nodeList
/collection (returned by getElementsByTagName()
and getElementsByClassName()
).
Incidentally, with jQuery:
$('.test > a').html('Hello world'); // sets the innerHTML of the returned elements
Or:
$('.test > a').text('Hello world'); // sets the text of the returned elements