I have several pages set up in the same way. Each page has about 10 to 15 images and if you click them, the image changes and becomes unclickable. The code I have for this is:
function ToggleOnclick(elID)
{
var el = document.getElementById(elID);
var loc = document.getElementsByClassName("wrapper");
if (el.onclick)
{
// Disable the onclick
el._onclick = el.onclick;
el.onclick = null;
el.src = loc.id + "/" + el.name + "Clicked.png";
}
}
The html for the images is:
....
<div class="content">
<div class="wrapper" id="som">
<div class="img0"><img src="som/doos.png" alt="doos" name="doos" id="doos" onclick="ToggleOnclick(this.name);" /></div>
<div class="img1"><img src="som/beer.png" alt="beer" name="beer" id="beer" onclick="ToggleOnclick(this.name);" /></div>
<div class="img2"><img src="som/bel.png" alt="bel" name="bel" id="bel" onclick="ToggleOnclick(this.name);" /></div>
....
Because I need to make about 20 html-files, I had the notion to add the source location of the images as the id-tag of the wrapper-div.
I have little knowledge of javascript and I have a hard time finding what I'm looking for. Possibly also due to not being a native speaker and not knowing what I'm looking for exactly.
Please bear in mind that my files:
tldr;I need a way to set the img src based on the id-tag of the wrapper-div
getElementsByClassName
gives a collection not a single element
pass the element to ToggleOnclick
so you won't have to fetch it in the function
function ToggleOnclick(el)
{
//var loc = document.getElementsByClassName("wrapper")[0];//assuming there is only one wrapper
if (el.onclick)
{
// Disable the onclick
el._onclick = el.onclick;
el.onclick = null;
//el.src = loc.id + "/" + el.name + "Clicked.png";
el.src = el.src.replace(".png", "Clicked.png");
}
}
<img src="som/doos.png" alt="doos" name="doos" id="doos" onclick="ToggleOnclick(this);" />