Change IMG title or alt with Javascript

I have a function that rolls out a new image if success:

function changeimage(img_name) {
document[img_name].src=‘/images/user/dot-success.gif’;
}

Is there anyway I can change the title (title=“”) or/and alt (alt=“”) of that same image?

Thanks
Ryan

document[img_name].alt='alt text';
 document[img_name].title='title text';

or

document[img_name].setAttribute('alt', 'alt text');
document[img_name].setAttribute('title', 'title text');