views:

79

answers:

3

I'm trying to pass my own array of objects: results[num_row] = {'title:\'' + title + '\', ' + 'url:\'' + url + '\''};

but this returns the error in firebug

when I try: results[num_row] = {title:'Link A', url:'/page1'}

it works.

Thanks,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
  <link rel="stylesheet" href="styles.css" type="text/css" />
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript" src="jquery.autocomplete.js"></script>
<script type="text/javascript">
var test = ["a","b","ab"];
var results = new Array();
function prep(){
$("#searchbox").autocomplete(results,{
  formatItem: function(item) {
    return item.title;
  }
}).result(function(event, item) {
  location.href = item.url;
});
}
$(document).ready(function(){
                $.ajax({
                    type: "GET",
                    url: "links2.xml",
                    dataType: "xml",
                    success: function(xml) {
                        // Count elements
                        var count = $(xml).find('ROW').length;
                        // Create Array of correct length
                        //window.results = new Array(count);
                        // Set array variable
                        var num_row = 0;
                        //data string
                        var datastring = "";
                        //start of find block
                        $(xml).find('ROW').each(function() {
                            var title = $(this).find('SC_DF_FIELD_1').text();
                            var url = $(this).find('SC_DF_FIELD_2').text();
                            var support_url = $(this).find('SC_DF_FIELD_3').text();
                            var description = $(this).find('SC_DF_FIELD_4').text();
                            var contacts = $(this).find('SC_DF_FIELD_5').text();
                            //clean up xml variables
                            url = url.substring(url.indexOf('>') + 1, url.indexOf('/a') - 1);
                            support_url = support_url.substring(support_url.indexOf('>') + 1, support_url.indexOf('/a') - 1); /*need to clean up contacts search later */

                            //alert(title + '\t' + url + '\t' + support_url + '\t' + description + '\t' + contacts);
                            results[num_row] = {'title:\'' + title + '\', ' + 'url:\'' + url + '\''};
                            //results[num_row] = title;
                            //results[num_row] = {text:'Link A', url:'/page1'}
                            num_row++

                          //  $('<div class="items"></div>').html('<a href="' + url + '">' + title + '</a>').appendTo('#page-wrap');
                        });
                        //end of find block
                        prep();
                    }
                });
});
</script>
</head>
<body>
    <div id="page-wrap">
<FORM autocomplete="off"><INPUT id="searchbox" type="text"/>
</FORM></DIV>
</body>
</html>
+2  A: 

That gives you a SyntaxError, the Object initializer syntax doesn't work like that.

If you want to use the title and url variables in a new object, you can easily:

//...
results[num_row] = {'title': title , 'url': url};
//...
CMS
I'm such a bone head...thank you! I'd like to also add the additional values I have so: `results[num_row] = {'title': title, 'url': url, 'support_url': support_url, 'description': description, 'contacts': contacts};`
specked
+1  A: 

Essentially when you write

{'title:\'' + title + '\', ' + 'url:\'' + url + '\''}

You are trying to set the value of

results[num_row]

equal to an incomplete object

{ PropertyName }

when you need

{ PropertyName : PropertyValue }
ChaosPandion
A: 

Try

results= [];
num_row = 0;
title = "myTitle";
url = "myURL";
results[num_row] = {'title': title, 'url': url}
Topera