views:

139

answers:

1

In my Rails app, I'm trying to hide a div (box) on page load in the Javascript function below. This function loops through a series of checkboxes with the same name (cb). If any of the checkboxes are checked, it should show the div.

function BoxCheck(cb,box){
var cbs=document.getElementsByName(cb);
var d=document.getElementById(box);
d.style.display = 'none'; 
var flag_check=0

for (var zxc0=0;zxc0<cbs.length;zxc0++){
  if (cbs[zxc0].checked){
    flag_check=flag_check+1
    } else
    { }
}
if (flag_check > 0){
    d.style.display = 'block'; 
    document.getElementById('multi_control_spacer').style.display = 'block';      
 } else {
    d.style.display = 'none'; 
    document.getElementById('multi_control_spacer').style.display = 'none'; 
 }
}

The function fires on load with:

<body onload="javascript:BoxCheck('doc_ids[]','multi_control');">

The problem is that when no checkboxes are selected, the div flashes on momentarily, and then disappears.

Here's the CSS:

#multi_control {
    padding: 10px;
    background: #333;
}

I tried setting the css to display:none, but then I can't seem to make it switch to back to display:block with Javascript.

+5  A: 

Why not? Have you tried:

element.style.display = 'block';

How about putting style="display:none" into the div tag so it's hidden to begin with?

karim79
Nice. For some reason I locked myself into setting the display attribute in the stylesheet.Your solution of putting it directly into the div works.Thanks!
Sol Irvine
then accept his answer, so we know the matter is closed
nasmorn