Go Back   SitePoint Forums > Forum Index > Program Your Site > JavaScript
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old Nov 7, 2009, 00:45   #1
digitalbart
SitePoint Enthusiast
 
digitalbart's Avatar
 
Join Date: Apr 2002
Location: chicago
Posts: 75
jquery ajax menu collapse expand

Hi I am trying to build something like a tree menu but not exactly. You click a menu, which then does a ajax request for submenus and then displays them. You can then click on the submenus again and get another level of submenus. My problem is sometimes when click on the submenus it does close the menu or sometimes will keep adding to the existing submenu, menaing it will add an identical copy of the submenu, so you see two of the everything. I am using .live to and then is.hidden to see if the menu is visible. Any thoughts as to how I can get this work, toggle can be used with .live as far as I know because you have to click twice?

JavaScript Code:
$(document).ready(function(){
 
 
  $('.cat',this).toggle(
    function () {
      var id = $(this).attr('href');
      if($('.box'+id).text() == '') { 
    var rand = '?random=' + Math.random();           
        $.getJSON('mclicks.php'+rand,{id:id},function(data){ 
          $.each(data, function(i,item){                               
            $.each(item, function(intIndex, objValue) {
             // console.log(this);
                 $("div.box"+id).append("<a class='ahref' href='" + intIndex + "' id='"+id+"' onclick='return false;'>" + objValue + "</a> <div class='sub" + intIndex + " data'></div><br /> ");
             });
            $("div.box"+id).addClass("data").slideDown(200);         
          })
        })
        // $(this).html("Hide Clicks");
      } else {
        // $(this).html('Hide Clicks'); 
        $('.box'+id).slideDown(200)
      } 
    }
    function () { 
      var id = $(this).attr('href');
      //$(this).html('View Clicks'); 
      $('.box'+id).slideUp(200)
    } 
  );
 
 
 
   $('.ahref',this).live("click", function(){
   
 
   // e.preventDefault(e);
     
   var id = $(this).attr('id');
   
   var subid = $(this).attr('href');
   
 
 
if($('div.sub'+subid).is(':hidden')) {
 
   
    var text1 = $('div.sub'+subid).length;
     
          var rand = '?random=' + Math.random();
               
        $.getJSON('mclicks.php'+rand,{subid:subid,id:id},function(data){ 
          $.each(data, function(i,item){                               
            $.each(item, function(intIndex, objValue) {
             // console.log(this);  + intIndex +   + intIndex +
                 $("div.sub"+subid).append("<a class='ahref' href='' onclick='return false;'>" + objValue + "</a> <div class='sub data'></div><br /> ");
             });           
          });
           
        })
       
      $(".sub"+subid).addClass("data").slideDown(200);
 
 
 } else {
      $('.sub'+subid).slideUp(200);
      $("div.sub"+subid).remove();
          var text1 = $('div.sub'+subid).length;
        console.log(text1);
 }
 
      }, function () { 
        // $(this).html('Hide Clicks'); 
        $('.sub'+subid).slideUp(100);
         $("div.sub"+subid).remove();
          var text1 = $('div.sub'+subid).length;
        console.log(text1);           
  });

HTML4Strict Code:
<a onclick="return false;" id="3" href="27" class="ahref">Car Washes</a>
 
<div class="sub27 data" style="display: block;"><a onclick="return false;" href="" class="ahref">Chevrolet</a> <div class="sub data"/><br/> <a onclick="return false;" href="" class="ahref">Soft Wash</a> <div class="sub data"/><br/> <a onclick="return false;" href="" class="ahref">Detail Hand Car Wash</a> <div class="sub data"/><br/> </div>
digitalbart is offline   Reply With Quote
 

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 02:47.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved