When clicking on an image, the image source should change, as well as the class of the matching span.
HTML:
<span class="oldClass">
Some Text
<img id="IdForThisImg" onclick="changeImage('IdForThisImg')" src="imgOld.png">
</span>
<span class="oldClass">
Some Text
<img id="IdForAnotherImg" onclick="changeImage('IdForAnotherImg')" src="imgOld.png">
</span>
I have tried numerous attempts for changing the class name with inspiration from "Adding and Removing Classes, with simple cross-browser JavaScript".
Javascript:
function changeImage(id)
{
element=document.getElementById(id)
if (element.src.match("imgOld"))
{
element.src="imgNew.png";
document.getElementByClassName("oldClass").className = "newClass";
}
else
{
element.src="imgOld.png";
document.getElementByClassName("newClass").className = "oldClass";
}
}
My problem appears to be that I am not getting the element that i want to change the class name of. Changing the image works perfectly, but I cannot change the class name.
Is it correct that the class name is not changing because I am not getting the correct element, if not could you point me in the direction of what it is? If it is correct, why is my current solution not working?
Additional info: I am using bootstrap twitter, using a , which I would like to change to "badge badge-default" when the image changes.