Results 1 to 4 of 4

Thread: Help with collapsable tree

  1. #1
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with collapsable tree

    I'm coding an expandable tree, with each collapsable link expanding when a plus sign is clicked, but I also want the tree to collapse when the name is clicked aswell, not just the plus sign.

    eg.

    [+]Explanations (want Explanations to be clickable too, not just plus sign)
    ----[+]Auto
    ....

    Javascript

    Code:
    <script type="text/javascript" language="javascript">
    function expandCollapseTree(obj){
    	var li = obj.parentNode;					
    	var uls = li.getElementsByTagName("ul"); 	 
    	var ul = uls[0];							
    	
    	if (ul.style.display == "none" || ul.style.display == ""){			
    		ul.style.display = "block";										
    		
    		obj.src = "images/minus_down.gif";				}
    		
    	else{
    		ul.style.display = "none";										
    		
    		obj.src = "images/plus_down.gif";			
    	}	
    }
    
    function swapImage(image){
    	var source = image.getAttribute("src");								
    	
    	if (source.indexOf("_up") !=-1){									
    		source = source.replace("_up", "_down");		
    	}
    	
    	else{
    		source = source.replace("_down", "_up");			
    	}
    	
    	image.setAttribute ("src", source);
    }
    HTML

    Code:
    <div id="treemenu">
        <ul id = "tree">
          <li><img src="images/plus_up.gif" onclick="expandCollapseTree(this)" onmouseover="swapImage(this)" onmouseout="swapImage(this)"/>Explanations
            <ul>
              <li><img src="images/plus_up.gif" onclick="expandCollapseTree(this)" onmouseover="swapImage(this)" onmouseout="swapImage(this)"/>Auto
                <ul>
                  <li>Alternative Fuel </li>
                  <li>Buying and Selling</li>
                  <li>Car Models</li>
                  <li>Car Options &amp; Accessories</li>
                  <li>Car Safety</li>
                  <li>Cutting Edge</li>
                  <li>Under the Hood </li>
                </ul>
              </li>
              <li><img src="images/plus_up.gif" onclick="expandCollapseTree(this)" onmouseover="swapImage(this)" onmouseout="swapImage(this)"/>Communication
                <ul>
                  <li>Conferencing</li>
                  <li>Desktop Fax</li>
                  <li>Notifications </li>
                </ul>
              </li>
              <li>Computer</li>
              <li>Electronics</li>
              <li><img src="images/plus_up.gif" onclick="expandCollapseTree(this)" onmouseover="swapImage(this)" onmouseout="swapImage(this)"/>Entertainment
                <ul>
                  <li>Arts</li>
                  <li>Movies</li>
                  <li>Music</li>
                  <li><img src="images/plus_up.gif" onclick="expandCollapseTree(this)" onmouseover="swapImage(this)" onmouseout="swapImage(this)"/>Sports
                    <ul>
                      <li>AFL</li>
                      <li>Rugby</li>
                      <li>Soccer</li>
                      <li>Tennis</li>
                    </ul>
                  </li>
                  <li>Television</li>
                  <li>Toys</li>
                  <li>Games</li>
                </ul>
              </li>
            </ul>
          </li>
          <li><img src="images/plus_up.gif" onclick="expandCollapseTree(this)" onmouseover="swapImage(this)" onmouseout="swapImage(this)" />Expert Reviews
            <ul>
              <li><img src="images/plus_up.gif" onclick="expandCollapseTree(this)" onmouseover="swapImage(this)" onmouseout="swapImage(this)"/>Consumer Guide Auto
                <ul>
                  <li>New Cars </li>
                  <li>Used Cars </li>
                </ul>
              </li>
              <li>Consumer Guide Products</li>
              <li><img src="images/plus_up.gif" onclick="expandCollapseTree(this)" onmouseover="swapImage(this)" onmouseout="swapImage(this)"/>Mobil Travel Guide
                <ul>
                  <li>Hotels</li>
                  <li>Resaurants</li>
                  <li>Spas</li>
                </ul>
              </li>
            </ul>
          </li>
          <li><img src="images/plus_up.gif" onclick="expandCollapseTree(this)" onmouseover="swapImage(this)" onmouseout="swapImage(this)" />Opinions
            <ul>
              <li>Member Home</li>
              <li>Log In/Register</li>
            </ul>
          </li>
        </ul>
      </div>
    Thanks

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Do you want it to have the hand icon? Becuase right now if I click on one, its right. It hides, but there's no hand...
    If you want the hand, add this style:
    Code:
    cursor:hand
    Otherwise, I don't know what you mean.
    Last edited by Nile; 09-23-2008 at 02:03 AM.
    Jeremy | jfein.net

  3. #3
    Join Date
    Sep 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It works when you click on the plus and minus signs, i wan't it to work when you click on the link title aswell.

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Just add an onclick event on the text itself (add highlighted):
    Code:
    <img src="images/plus_up.gif" onclick="expandCollapseTree(this)" onmouseover="swapImage(this)" onmouseout="swapImage(this)"/><span onclick="expandCollapseTree(this)">Explanations</span>
    ...do the same thing on the other texts.

    Hope that helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •