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?
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With