I’m working on a page that was created by someone else, and which uses Javascript to apply CSS style changes to certain elements on the page. Basically we have a home-built CMS that lets you browse the web directory and edit files. You select a file be ticking a checkbox next to the filename, and the aforementioned Javascript “unhides” the buttons to edit, rename, or delete a file.
The Javascript is written in a way that if you select only one file, you have all options (edit, rename, and delete) but if you select more than one, you can only delete. It works very well, EXCEPT when there is only one item (file or subdirectory) in a directory, in which case you can’t unhide any buttons. Here’s the script in question.
<script type="text/javascript">
function hideButtons()
{
document.getElementById('edit').style.visibility="hidden";
document.getElementById('rename').style.visibility="hidden";
document.getElementById('delete').style.visibility="hidden";
}
function showOptions()
{
var thisItemChecked = 0;
if (document.getElementsByName('thisItem').length==1)
{
if (document.getElementById('thisItem').checked==true)
{
thisItemChecked++;
}
} else if (document.getElementsByName('thisItem').length>1) {
for (var i=0; i<document.editTopic.thisItem.length; i++)
{
if (document.editTopic.thisItem[i].checked==true)
{
thisItemChecked++;
}
}
}
if (thisItemChecked==1)
{
document.getElementById('edit').style.visibility="visible";
document.getElementById('rename').style.visibility="visible";
} else {
document.getElementById('edit').style.visibility="hidden";
document.getElementById('rename').style.visibility="hidden";
}
if (thisItemChecked>=1)
{
document.getElementById('delete').style.visibility="visible";
} else {
document.getElementById('delete').style.visibility="hidden";
}
}
</script>
The HTML (all three buttons are like this):
<input type="image" name="edit" id="edit" src="images/edit_30x30.png" alt="Edit" title="Edit" />
And the checkbox that triggers the Javascript is like this:
<input type="checkbox" name="thisItem" id="thisItem#name#" value="#showDir##name#?type=#thisType#" onclick="showOptions();" />
(the funny #things# are ColdFusion variables, so you can ignore them)
The first function hides the buttons, and the other function does the rest. But why oh why won’t it work when there’s only one item in the directory?