Can't change src attribute reliably in IE6

Sample code:

<form name="myForm">
<a href="javascript:swapImage()"><img id="img1"
src="image1.jpg"></a>
</form>

function swapImage()
{
document.getElementById("img1").src="image2.jpg";
}

Problem

The issue is that changing the 'src' attribute of an <image> tag, is
making the image disappear, rather than change. Frustratingly, the
problem only happens in IE6.


Solution
Replace the javascript action in href with "#"

<form name="myForm">
<a href="#" onclick="swapImage();"><img id="img1"
src="image1.jpg"></a>
</form>

function swapImage()
{
document.getElementById("img1").src="image2.jpg";
}

0 comments: (+add yours?)