views:

189

answers:

3

I'm using the jQuery Autocomplete plugin and would like to store all the autocomplete values locally in an array. I'm able to retrieve a comma separated list from a url using jQuery.load() or jQuery.get(). However, I can't load the data into an array successfully.

Any suggestions on how to get this working? I realize there's probably a much better way. Any help would be appreciated.

Thanks!

A: 

When you say "comma separated list" I assume you mean a string (like "foo1, foo2, foo3")?

If so, you can use the split function of the string type like so:

var data = "foo1, foo2, foo3";
data.split(",");
// ["foo1", "foo2", "foo3"]
Bryan Matthews
A: 

Are you sure that it isn't working properly, but that you're not trying to access that variable outside of the async ajax call?

The only way to have immediate access outside of that ajax call is to run it async=false, or to use a timer to wait and check for that value.

If this is the case, look to the docs on the ajax abstractions:

http://api.jquery.com/jQuery.ajax/

Specifically:

var html = $.ajax({
  url: "some.php",
  async: false
 }).responseText;
ScottE
Thank you so much! This worked perfectly. I continued troubleshooting after my original post and was able to figure out part (but not all) of this out. Your post was quite helpful. I did have a look at these docs, but I'm not quite up to speed yet on my jQuery ajax calls.Thanks again.
Moe
@Moe - mark the answer as correct so you can help others as well.
ScottE
A: 

Hi,I want to store auto complete values in array to store in DB. Any suggestions

function lookup(inputString) {
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
        $.post("rpc.php", {queryString: ""+inputString+""}, function(data){
            if(data.length >0) {
                $('#suggestions').show();
                $('#autoSuggestionsList').html(data);
            }
        });
    }
} // lookup

function fill(thisValue) {

if($('textarea#inputString1').val() == ''){

<!-- $("textarea[inputString1]").prependTo("#foo"); -->
$('#inputString').val(thisValue);
        $('#inputString1').val(thisValue);
        }else{
        $('textarea#inputString1').val(thisValue)=$('textarea#inputString1').val()+','+$('#inputString').val();
        }
    setTimeout("$('#suggestions').hide();", 200);

}
Type your county:
        <div class="suggestionsBox" id="suggestions" style="display: none;">
            <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
            <div class="suggestionList" id="autoSuggestionsList">
                &nbsp;
            </div>
        </div>
                    <textarea type="text" size="30" value="" id="inputString1"  /></textarea>
Rambau D