-1

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()});

}

2
  • 1
    Please format your source code! You can do it by adding four blank spaces in front of each row. Commented Jul 2, 2009 at 10:01
  • Please post your complete original code. This one has many JavaScript errors Commented Jul 2, 2009 at 10:04

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Isn't it required to increment value of 'i'
Wow, that was quick :D Thank you good Sir!! I'll be back after I figured out what else is wrong with my code ;)
didn't work for me :( edited to add full source...the function that's having issues is at the bottom. also pastebin.com/m2d28a15d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.