2

this is the code i have:

<?
    while ($row1 = mysql_fetch_object($result1)) {
       echo '<a href="#" onclick="showhide("'.$row1->id.'");">Name</a>';
       while ($row2 = mysql_fetch_object($result2)) {
          echo '<div id="'.$row1->id.'">';
          echo 'blabla';
          echo '</div>';
       }
    }
?>
    <script> 
    var state = 'none';
    function showhide(layer_ref){
    if(state == 'block'){
    state = 'none';
    }else{
    state = 'block';
    }
    if(document.all){ //IS IE 4 or 5 (or 6 beta)
    eval("document.all." +layer_ref+ ".style.display = state");
    }
    if (document.layers) { //IS NETSCAPE 4 or below
    document.layers[layer_ref].display = state;
    }
    if (document.getElementById &&!document.all) {
    hza = document.getElementById(layer_ref);
    hza.style.display = state;
    }
    }
    </script>

this outputs something like:

Name
  blabla
Name
  blabla
...

and when clicking on the head (name) it should toggle it contents (show/hide) but it doesn't. what am i doing wrong?

1
  • 3
    Do not use eval(), please! Commented Apr 25, 2013 at 9:42

1 Answer 1

3

This might work for you:

<script language="JavaScript">
    function toggle(id) {
        var state = document.getElementById(id).style.display;
            if (state == 'block') {
                document.getElementById(id).style.display = 'none';
            } else {
                document.getElementById(id).style.display = 'block';
            }
        }
</script>

Live demo here>>

Sign up to request clarification or add additional context in comments.

1 Comment

mmh, when inspecting the elements, onclick="toggle("'.$row1->project_id.'");" outputs: onclick="toggle( "18");" (include strange space before 18), it only displays the heads (Name, Name, ..) with a big lot of whitespace between them...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.