im trying to get a image to change everytime its clicked, visa-versa. now it will change once, but thats it. Its driving me mad, any ideas where im going wrong?
thanks
<script type="text/javascript">
function toggle_visibility(id) {
var f = document.getElementById('helptext2');
if(f.src == 'assets/expand_help.png')
f.src = 'assets/expand_help.png';
else
f.src = 'assets/expand_help_down.png';
}</script>
Aside from the logic problem there’s the “src contains the full path” issue. Also if you’re going to pass an ID you may as well use it, and why not make the function reusable?:
<script type="text/javascript">
function toggle_visibility(id, a, b)
{
var f = document.getElementById( id );
f.src = ( f.src.indexOf( a ) > -1 ) ? b : a;
}
</script>
<a href="#" onClick="toggle_visibility('helptext2', 'assets/expand_help.png', 'assets/expand_help_down.png')">
<img src="assets/expand_help.png" id="helptext2" /> change image </a>
thank you both for your help, worked great.
but then i thought i might try optimise my code i have elsewhere.
Basicly you click the image, it changes and also hides/unhides a div id…
I tryed to reference the code you gave me, and tryed a few changes but i can get it to show (from its default hidden). but i cant get it to hide again etc.
<script type=“text/javascript”>
function toggle_visibility(id, a, b)
{
var f = document.getElementById( id+'img' );
var e = document.getElementById( id );
f.src = ( f.src.indexOf( a ) > -1 ) ? b : a;
e.style.display = ( e.style.display > -1 ) ? 'none' : 'block';
}
</script>