Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change image alt with Javascript onclick

I have a thumbnail image that when clicked changes a larger image on the page. I have that part of the code working by just changing the .src with onclick. Is there also a way to change the alt and title attributes with onclick?

like image 490
computersaurus Avatar asked Jul 10 '10 20:07

computersaurus


1 Answers

You can use setAttribute or set the property directly. Either way works, the setAttribute is the standard DOM way of doing it though.

el.onclick = function() {
    var t = document.getElementById('blah');

    // first way
    t.src = 'blah.jpg';
    t.title = 'new title';
    t.alt = 'foo';

    // alternate way
    t.setAttribute('title', 'new title');
    t.setAttribute('alt', 'new alt');
    t.setAttribute('src', 'file.jpg');
}
like image 99
meder omuraliev Avatar answered Sep 24 '22 23:09

meder omuraliev