user132192
user132192

Reputation: 47

jQuery javascript array handling

Noobie muddling his way through an Ajax autosuggest form:

I've got most of it working, but need to break things down a bit, so I can understand how jQuery refers to things. EDIT: This is my working code, thanks guys. Some problems with delay on the ajaxdiv, not hanging around, but I am working on that ;)

function jQajax(ipfield,ajd,dbtable,company,select,element){

if (!ipfield || !ajd || !dbtable || !company || !select || !element){alert("Parameters not sent to ajax handler correctly; Input to act on, Ajax Div, Database table to check, Company (database name), Field to select, Element Id. "); return false;}

actobj="#"+ipfield;// set the active object for jQuery

ajdiv="#"+ajd; //set the ajax responding div

listindex=-1; //clear the notion of which item is selected

scriptgo=1; // slowdown for key javascript

leftpos = findPos($(actobj));

var width = $(actobj).width()-2;

$(ajdiv).css("left",leftpos); 

$(ajdiv).css("width",width);

$(actobj).keyup(function(event){





     //alert(event.keyCode);

     //Key presses you need to know: 40=down 38=up 13=Enter 27=ESC 8=Bkspc 46=DEL

     var keyword = $(actobj).val();

     if(keyword.length)

     {

         if(event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13)

         {



             $.ajax({

               type: "GET",

               url: "includes/ajax_server.php",

               cache: false,

               data: "company="+company+"&data="+keyword+"&table="+dbtable+"&select="+select,

               success: function(msg){  

                if(msg != 0){

                  $(ajdiv).fadeIn("slow").html(msg);

                    fader();

                }else{

                  $(ajdiv).fadeOut("slow"); 

                                   }

                               }

             });

         }

         else

         {

            switch (event.keyCode)

            {

            case 40: // down pressed

                 {

                fader();     

                  step=1;

                  mvIndex(step);



                 }

             break;

             case 38: //up pressed

             {

                 fader();

                    step=-1;

                  mvIndex(step);

             }

             break;

             case 13:

                {

                 $(actobj).val($(".ajitems[class='selected'] a").text());

                 listindex=-1;

                loadSkuDetails(element);

                $(ajdiv).fadeOut("slow");

            }

             break;

             case 27:

             {

             listindex=-1;

                $(ajdiv).fadeOut("slow");

                $(actobj).focus();

            }

            }

         }

     }

     else

        $(ajdiv).fadeOut("slow");

});

$(ajdiv).mouseover(function(){

    $(this).find(".ajitems a:first-child").mouseover(function () {

          $(this).addClass("selected");



                });

    $(this).find(".ajitems a:first-child").mouseout(function () {

          $(this).removeClass("selected");

    });

    $(this).find(".ajitems a:first-child").click(function () {

          $(actobj).val($(this).text());

          loadSkuDetails(element);

          $(ajdiv).fadeOut("slow");

    });

});

};

function findPos(obj) { //find the REAL position of the parent object, especially useful when scrolling can occur

var curleft = curtop = 0;

if (obj.offsetParent) {

do {

        curleft += obj.offsetLeft;  

} while (obj = obj.offsetParent);

return [curleft];

}

}

jQuery.fn.delay = function(time,func){ //run a delayed function

return this.each(function(){

    setTimeout(func,time);

});

};

function mvIndex(step){

                    if(scriptgo==1){        

                    kids=$(".resultlist").children();



                    $(".resultlist").children().each(function(i){



                    if ($(this).hasClass("selected")){

                    listindex = i;console.log(listindex);

                        }

                    });



                    if (listindex==-1 && step==-1)change=i-1;//up = last item

                    if (listindex==-1 && step==1)change=0;//down = first item

                    if (listindex > -1){

                        change=listindex+step; //already selected

                        if (change > i-1 || change < 0) change=0;

                    }

                    console.log("mv2",listindex,"step",step,"change",change);

                    if (change >=0)$(".resultlist").children("*").eq(change).addClass("selected");

                    if (listindex >=0)$(".resultlist").children("*").eq(listindex).removeClass("selected");

                    scriptgo=0;

                    slowDown();

                    }   

              }

function slowDown(){

$(actobj).delay(1000, function(){scriptgo=1;});}

function fader(){

$(ajdiv).delay(10000, function(){$(ajdiv).fadeOut()});

}

Upvotes: -1

Views: 1324

Answers (1)

RedWolves
RedWolves

Reputation: 10385

I think your are trying to iterate through the selection with a for loop instead of using the jQuery Core function $.each()

$(".resultlist").children().each(function(i){
  if ($(this).hasClass("selected")){
    listindex = i;
  }
});

The code above should do almost the exact same as what your code is trying to do.

Upvotes: 5

Related Questions