views:

251

answers:

1

Hi,

I have problem in adding autocomplete plugin to multiple text inputs in loop (no errors are returned).

// get text inputs to attach autocomplete    
var locinputs = $('#localizations').find('input:text');

// iterate over elements and add autocomplete plugin
        for (var i = 0; i < locinputs.length; i++) {
           // asp.mvc array with special characters replacing 
            var locNameField = locinputs[i].name.replace('[', '\\\\[');
            locNameField = locNameField.replace(']', '\\\\]');
            locNameField = locNameField.replace('.', '\\\\.');

            $('input#' + locNameField).autocomplete('<%=Url.Action("GetCity", "Localization") %>', {
                extraParams: {
                    provinceId: function () { return 21; }
                },
                dataType: 'json',
                parse: function (data) {
                    var rows = new Array();
                    for (var i = 0; i < data.length; i++) {
                        rows[i] = { data: data[i], value: data[i].PlaceId, result: data[i].Name };
                    }
                    return rows;
                },
                formatItem: function (row, i, n) {
                    return row.Name;
                },
                width: 300,
                mustMatch: true,
                multiple: true
            });
        }

There is no error on loading, I tried to debug using Firebug, elements are accessible by jquery selector

<input type="text" name="loc[0].CityNames" id="loc[0].CityNames" value="" />

corrected statement $('#localizations input:text').each(function () {

            $(this).autocomplete('<%=Url.Action("GetCity", "Localization") %>', {
                extraParams: {
                    provinceId: function () { return 21; }
                },
                dataType: 'json',
                parse: function (data) {
                    var rows = new Array();
                    for (var i = 0; i < data.length; i++) {
                        rows[i] = { data: data[i], value: data[i].PlaceId, result: data[i].Name };
                    }
                    return rows;
                },
                formatItem: function (row, i, n) {
                    return row.Name;
                },
                width: 300,
                mustMatch: true,
                multiple: true
            });
        });
+1  A: 

Try to use jQuery each instead of using the for loop.

$('#localizations input:text').each(function() {
    // Your code goes here.
});

Also make sure that selector works right. Either debug in firebug and inspect the $('#localizations').find('input:text') expression or just alert it's size.

Juriy
I moved code to each jquery statement and id doesn't work. But when I try to access element $("input#loc\\[0\\]\\.CityNames"); it is accessible (from firebug).<input type="text" name="loc[0].CityNames" id="loc[0].CityNames" value="" />
marcinn
Try to put the alert into the each: is it called the correct number of times?One more thing: in each function "this" will refer to the DOM element. So to manipulate with current element try using $(this).attr("name") etc.
Juriy
Juriy thanks for jquery and $(this) :)
marcinn