I'm making a really simple photo gallery with thumbnails and I have a working code which changes the big image when a thumbnail is clicked. Now what I need is to add a green border to the thumbnail which is currently active.
HTML:
<div id="gallery"> <div id="big"> <img align="center" src="big/pipe_big.jpg" alt="" id="image" /> </div> <div id="small"> <img onclick="changeImage('pipe')" src="small/pipe_small.jpg" alt="" /> <img onclick="changeImage('leaves')" src="small/leaves_small.jpg" alt="" /> <img onclick="changeImage('orange')" src="small/orange_small.jpg" alt="" /> <img onclick="changeImage('xuangong')" src="small/xuangong_small.jpg" alt="" /> </div> <div id="small"> <img onclick="changeImage('grave')" src="small/grave_small.jpg" alt="" /> <img onclick="changeImage('lotus')" src="small/lotus_small.jpg" alt="" /> <img onclick="changeImage('tibet_girl')" src="small/tibet_girl_small.jpg" alt="" /> <img onclick="changeImage('girl_water')" src="small/girl_water_small.jpg" alt="" /> </div> </div> JS:
function changeImage(x){ var image = document.getElementById('image'); var active_image = ?????????? if (x == 'pipe') { image.src = 'big/pipe_big.jpg'; } else if (x == 'leaves') { image.src = 'big/leaves_big.jpg'; } else if (x == 'orange') { image.src = 'big/orange_big.jpg'; } else if (x == 'xuangong') { image.src = 'big/xuangong_big.jpg'; } else if (x == 'grave') { image.src = 'big/grave_big.jpg'; } else if (x == 'lotus') { image.src = 'big/lotus_big.jpg'; } else if (x == 'tibet_girl') { image.src = 'big/tibet_girl_big.jpg'; } else if (x == 'girl_water') { image.src = 'big/girl_water_big.jpg'; } active_image.style.border = '2px solid green'; } So I need to find the element that triggered the function and put it into variable "active_image" so that the function "changeImage()" always changes the border to 2px solid green. And please no jQuery solutions, I need it to be JavaScript.
addEventListener, not HTML.