Toggle control with dual function

Discussion in 'JavaScript' started by koolsamule, Dec 3, 2009.

  1. #1
    Hi Guys,

    I have a Javascript function that toggles a 'show/hide' table row:

    //CONTROL
    <a href="#" onclick="toggle('ROW 1, this)"><img src="../../Images/plus.gif" border="0" /></a>
    HTML:
    //JAVASCRIPT
    function toggle(id, obj) {
       var matchingElements = new Array();
       
       if (document.getElementsByClassName) {
          matchingElements = document.getElementsByClassName(id);
       } else {
          var elements = document.getElementsByTagName("tr");
          for (i = 0; i < elements.length; i++) {
             if (elements[i].className == id) {
                matchingElements[matchingElements.length] = elements[i];
             }
          }
       }
       
        for (i = 0; i < matchingElements.length; i++) {
          toggleDisplay(matchingElements[i], obj);
       }
    }
    
    function toggleDisplay(element, obj) {
       if (element.style.display == "none") {
          element.style.display = "block";
          obj.innerHTML = "<img src=\"../../Images/minus.gif\" border=\"0\">";
       } else {
          element.style.display = "none";
          obj.innerHTML = "<img src=\"../../Images/plus.gif\" border=\"0\">";
       }
    }
    Code (markup):
    Which works fine, but what I need is something like this:

    Control 1:
    Toggle - ROW 1 (+ Close ROW 2 (if visible))

    Control 2:
    Toggle - ROW 2

    Is this a Javascript or PHP issue?
     
    koolsamule, Dec 3, 2009 IP