Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  77] [ 1]  / answers: 1 / hits: 164050  / 14 Years ago, thu, january 27, 2011, 12:00:00

I have the following script which works with a 1 dimensional array. Is it possible to get this to work with a 2 dimensional array? Then whichever item is selected, by clicking on a second button on the page, should display the id of whichever item is selected.



This is the script with the 1 dimensional array:



var $local_source = [c++, java, php, coldfusion, javascript, asp, ruby];
$(#txtAllowSearch).autocomplete({
source: $local_source
});


This is the script for the button to check the id, which is incomplete:



$('#button').click(function() {
// alert($(#txtAllowSearch).someone_get_id_of_selected_item);
});

More From » jquery

 Answers
19

You need to use the ui.item.label (the text) and ui.item.value (the id) properties



$('#selector').autocomplete({
source: url,
select: function (event, ui) {
$(#txtAllowSearch).val(ui.item.label); // display the selected text
$(#txtAllowSearchID).val(ui.item.value); // save selected id to hidden input
}
});

$('#button').click(function() {
alert($(#txtAllowSearchID).val()); // get the id from the hidden input
});


[Edit] You also asked how to create the multi-dimensional array...



You should be able create the array like so:



var $local_source = [[0,c++], [1,java], [2,php], [3,coldfusion], 
[4,javascript], [5,asp], [6,ruby]];


Read more about how to work with multi-dimensional arrays here: http://www.javascriptkit.com/javatutors/literal-notation2.shtml


[#94028] Tuesday, January 25, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zayne

Total Points: 366
Total Questions: 98
Total Answers: 98

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
;