SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast digitalbart's Avatar
    Join Date
    Apr 2002
    Location
    chicago
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    live and toggle misfiring

    Hello

    I have a ajax menu that fetches data and returns it in json and then rebinds it to the page with live. This is very similar to a tree menu but it is text instead and horizontal This works great for the most part but sometimes it misfires when using .is(':hidden') and it ends up getting the data again and no matter what it will just keep adding the same data over instead of toggleing and closing the menu. Any ideas?

    Below is the code I am using.

    Code JavaScript:
     
    $(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(100);         
               });  
             });  
             // $(this).html("Hide Clicks");
           } else { 
             // $(this).html('Hide Clicks');  
             $('.box'+id).slideDown(100);  
           }  
         },  
         function () {  
           var id = $(this).attr('href');
           //$(this).html('View Clicks');  
           $('.box'+id).slideUp(100);  
         }  
       );
     
     
     /******************************* display menu for subcats ***********************************/
     
        $('.ahref',this).live("click", function(e){
     
     
        // e.preventDefault(e);
     
        var id = $(this).attr('id'); 
        var subid = $(this).attr('href');
     
        // console.log(id);
     
         if($('.sub'+subid).is(':hidden')) {
     
         if($('.sub'+id).text() == '') {     
           //$("div.sub"+id).append("sub menus goes here");
               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(100); 
     
          } // end of blank text check...
      } else {
           $('.sub'+subid).slideUp(100); 
           $("div.sub"+subid).html("");
      }
     
           }, function () {  
             // $(this).html('Hide Clicks');  
             $('.sub'+subid).slideUp(100);    
       });
     
     
     /******************************* end of menu for subcats ***********************************/  
     
     
     });


    Below is the code to generate the first round of links:

    The output is this:

    Code HTML4Strict:
    <a href="16" class="cat">Cat 1</a><br><div class="box16"></div>
    <a href="3" class="cat">Cat 2</a><br><div class="box3"></div>

  2. #2
    SitePoint Enthusiast digitalbart's Avatar
    Join Date
    Apr 2002
    Location
    chicago
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just curios if anyone has any thoughts? do I need to do more elaborate error checking on .is(hidden) or change it to .is(visible)?


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
  •