The problem is the way you get image source (document.getElementById.("showHide").src == "Hide Table.bmp")). The return value may include all the domain name, so it will never be equal to what you are trying to do. You can try...
if (document.getElementById.("showHide").src.match(/Hide[^\.]*\.bmp/i)) {
}
Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
What browser are you using? I am guessing you are using IE. It is a known bug that IE does not allow you to dynamically change the value of visibility via JavaScript. The way to workaround this is to use CSS. Create a class in CSS for show and hide, then use JavaScript to swap the class name.
Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
Ashton:
Yes and no. Yes if you are keeping your original format. No if you want a better improvement.
1)You should put script inside head tag even though there is nothing wrong putting it in the body tag.
2)You should put style tag inside head tag as well.
3)The way you do would not work if both of your images have not been loaded on the page already. You could load them up and hide them somewhere in the page, and then use the tag you are working to show it. Or you dynamically change the DOM, but that may be slow for big image.
I modified your code as below...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<style type="text/css">
.hideImg {
display: none;
visibility: hidden;
}
/* You do not really need this showImg class, but I put in just be more intuitive */
/* If you want to display, just give no class name to the image */
.showImg {
}
</style>
<script LANGUAGE="JavaScript">
function showHideTable() {
if (document.getElementById.("showHide").className.match(/hideimg/i)) {
document.getElementById("TableRow").className = "";
document.getElementById("showHide").src = "Show Table.bmp";
} else {
document.getElementById("TableRow").className = "hideImg";
document.getElementById("showHide").src = "Hide Table.bmp";
}
}
</script>
</head>
<body>
<img id="showHide" src="Hide Table.bmp" onclick="showHideTable()">
<table border=1 cellpadding=4 CELLSPACING=0>
<tr>
<th>ROW 2 - HEADER</th>
</tr>
<tbody id="TableRow" style="display:none">
<tr><td colspan="2">ROW 2 SubDATA...</td></tr>
<tr><td colspan="2">ROW 2 SubDATA1...</td></tr>
</tbody>
</table>
<!-- add this image for preload when you swap image later -->
<div class="hideImg">
<img src="Show Table.bmp">
<img src="Hide Table.bmp">
</div>
</body>
</html>
Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
You may break down the function into 2 functions if you want, but you need to deal with them properly.
The problem with visibility is what you are encountering in IE. IE has problem with changing style.visibility dynamically. Also, for style.display, it hide the display but the element is still in place! In other words, if you are displaying something in the middle of the page, and you use style.display="none", the element is hidden but there will be a blank in the middle of your page. The visibility is what should be used. Though, display is a fall back sometimes.
Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239