0

I want to pass value of i as a parameter for autosuggest. My intention is to pass value of i starting from 1960 to till date.

         <script>

            var i=1960;
            var currentTime = new Date();
            var year = currentTime.getFullYear();
            //document.write("The year is " + year);
            while (i <= year)
            {
                //document.write("The year is " + i);
                //document.write("<br />");

                $("#txtYear").autocomplete([+i], {
                width: 100,
                //multiple: true,
                mustMatch: true,
                matchContains: true,
                //formatItem: formatItem,
                //formatResult: formatResult
                });
                i++;
            }
                      });
                    </script>

Please help me out. Thank you so much in advance.

2 Answers 2

4

Your code is adding autocomplete to the elements 40+ times!

Anyways, jQuery autocomplete accepts an array for data. Why not build the array beforehand? Here's a sample. I have to say jQuery is picky with the data. They have to be strings.

var i = 1960,                          //first year
    currentTime = new Date(),          
    year = currentTime.getFullYear(),  //latest year
    data = [];                         //year array

while (i <= year){                     //construct year array
    data.push(''+i++);                 //minor string conversion by concatenating
}

$("#txtYear").autocomplete({
    source: data                       //use year array
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the quick reply. But in my case its not working. :(
What actually I want is , when I am typing 19 , then textbox automatically should start suggesting the years. Its working in the case of month and date. my code for date is as follows :
$("#txtMonth").autocomplete(["1","2","3","4","5","6","7","8","9","10","11","12"], { width: 100, //multiple: true, mustMatch: true, matchContains: true, //formatItem: formatItem, //formatResult: formatResult });
Thanks... its working... There was a mistake from my side... Your code is perfect... Thanks once again... :)
1

You first need to create an array of the values you want, then set it in the autocomplete like this:

    $( "#tags" ).autocomplete({
        source: years
    });

Comments

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.