Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  17] [ 2]  / answers: 1 / hits: 15113  / 11 Years ago, fri, august 9, 2013, 12:00:00

There are 5 no. of buttons(images).
Initially all are off image. Only 1 may be on at a time.
So when i press any button that img's src changes to on.png. Then when I press any of those on or off buttons, the pressed button source img changes to on.png and all other on img also change to off.png.



The html code is,



    <table cellspacing=0 style=padding:0%; margin:0% auto;>
<tr><td><img id=img1 src=off.png height=30 width=30 onclick=off(this.id); /></td><td>45:78</td></tr>
<tr><td><img id=img2 src=off.png height=30 width=30 onclick=off(this.id); /></td><td>45:78</td></tr>
<tr><td><img id=img3 src=off.png height=30 width=30 onclick=off(this.id); /></td><td>45:78</td></tr>
<tr><td><img id=img4 src=off.png height=30 width=30 onclick=off(this.id); /></td><td>45:78</td></tr>
<tr><td><img id=img5 src=off.png height=30 width=30 onclick=off(this.id); /></td><td>45:78</td></tr>
</table>


The javascript code is,



    function off(a)
{
var images = document.getElementsByTagName('img');
for(var i = 0; i < images.length; i++)
{
var img = images[i];
alert(img.src);
if(img.src == 'on.png')
{
img.src = 'off.png';
}
}
document.getElementById(a).src='on.png';
}


The if() condition is not working, Please provide solution and explain why its not



working.
Thank you!


More From » html

 Answers
3

img.src will return the full path of the image (/full/path/to/on.png) rather than what the src attribute is set to in the markup (on.png). Instead use:



if (img.getAttribute('src') === 'on.png')

[#76430] Thursday, August 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ammonderekm

Total Points: 247
Total Questions: 105
Total Answers: 98

Location: Tuvalu
Member since Sat, Feb 11, 2023
1 Year ago
;