The autocomplete function is based upon this tutorial http://www.bewebdeveloper.com/tutorial- ... and-jquery. You can download the sourcecode there. For noobs like me I changed the fields:
country_id to ean_id
country_list_id to *drumroll* ean_list_id
Full scripts are located near the end of page 2. Enjoy
EDIT: Why does the set_item(item) function only work if result.x is a number and not string ???? I'm going insane over this :/
Edit again: Well I forgot this little f****er ' ... there was 3 hours of my life I never get back heh
Code: Select all
text += "<li onclick=\"set_item(" + result[i].name + ")\">" + result[i].ean + "</li>";
should be
text += "<li onclick=\"set_item('" + result[i].name + "')\">" + result[i].ean + "</li>";Hi, I'm very new to both javascript and jquery.
I have a php function that echo a json encoded array (result[]) to the autocomplet() function.
the array is in form [0](ean=>, name=>, and so on=>)
the below function works where i send a specific value (like result[1].ean) to the set_item() function but I want the set_item function to update multiple fields on my page so I would like to pass an array like result[1] or result[3] to the set_item function and from within the set_item function extract the specific values like result[1].ean or result[1].name.
I cant get it to work when trying to pass an array..
Code: Select all
function autocomplet() {
var min_length = 0; // min caracters to display the autocomplete
var keyword = $('#ean_id').val();
if (keyword.length >= min_length) {
$.ajax({
url: 'ajax_php/ajax_browserefresh.php',
type: 'POST',
data: {keyword:keyword},
success:function(data){
var result = JSON.parse(data);
var text = "<li onclick=\"set_item(" + result[0].ean + ")\">" + result[0].ean + "</li>";
$('#ean_list_id').show();
if(result[0] == -1){ $('#ean_list_id').hide();}
for (i = 1; i < result.length; i++) { //från 1 eftersom jag redan satt 0 ovan
text += "<li onclick=\"set_item(" + result[i].ean +")\">" + result[i].ean + "</li>";
}
$('#ean_list_id').html(text);
$('#name_id').html(text);
//$('#ean_list_id').html(result[1].ean);
//$('#name_id').html(result[1].ean);
}
});
} else {
$('#ean_list_id').hide();
}
}
// set_item : this function will be executed when we select an item
function set_item(item) {
// change input value
$('#ean_id').val(item);
// hide proposition list
$('#ean_list_id').hide();
$('#name_id').show();
}
Code: Select all
var text = "<li onclick=\"set_item(" + result[0] + ")\">" + result[0].ean + "</li>";I tried to json encode result[0] and then parse it within the set_item() function but it didnt work..
so is there a special trick to send arrays to functions in javascript?
EDIT: Also for some reason this function only works when result.something is a number but not string??? So .ean works .netweight works but .name does not work ??