Accessing a list after AJAX call
I have a page that creates one unordered list from the contents of another list. The second list is created from a database query and is hidden on the page. I have written a JQuery function shown below that does an AJAX call which causes the hidden list to be created and that is working perfectly. The problem is that when the code following the AJAX call is executed it doesn't see the hidden list. The alert message displays 0 the first time. clicking on the Load button a second time results in the first list being displayed. If I click on the Load button a third time, the previously loaded list is displayed; click on a fourth time and the new list is displayed.
In other words, it takes two cycles to display the correct list. I am at a loss as to what might be the problem. Other than this issue the code does what I need it to do.
Any ideas or suggestions?
Thanks in advance.
Chuck
- $(function() {
- var idealist = ''; // Contains the name of the selected idealist
- var listid = ''; // Contains the record id in the idealist table for the idealist selected
- var item = $('#itemselected');
-
- // Get the idea list name from the dialog box list of idea lists.
- $("#selectlist").change(function(){
- $("option:selected", $(this)).each(function(){
- idealist = $(this).text(); // the selected option text is the name of the list
- listid = $(this).val(); // the selected option value is the list id in the idealist table
- $(item).html('Idea list to be loaded: ' + idealist);
- });
- }).trigger('change');
-
- // The user has selected an idea list from
- // the dialog box so process it.
- var execute = function() {
- // Send the id of the idea list to the
- // loadideas method of the design controller.
- $.ajax({
- type: 'POST',
- url: 'design/loadideas',
- data: {idealist: listid},
- dataType: 'text',
- success: function(){
- $('#idealist-gallery').load('design/getFabrics');
- }
- });
-
- // Clear the list and headings
- $('#idealist-subhead').empty();
- $('#idealist').empty();
-
- // Show the idea list name in the subheading
- $('#idealist-subhead').append('<span>'+idealist+'</span>');
-
- // Create the idea list from the idealist-gallery
- var fabriclist = $('#gallery > li', '#idealist-gallery');
- alert($(fabriclist).size());
- $(fabriclist).each(function(){
- var caption = $(this).find('a').attr('title');
- var image = 'li#'+$(this).attr('id')+' a';
- var thumb = '<img id="idea-image-' + $(this).attr('id') + '" class="idea-image'
- + '" src="'+$(image).attr('href')
- + '" title="'+ caption + '" alt="'+ caption + '" width="20" height="20" />';
- $('#idealist').append($('<li class="idealist-item"'
- + ' id="' + 'idealist-item-' + $(this).attr('id') + '">'
- + thumb + '<div>' + caption + '</div></li>'));
-
- // SET UP DRAGGABLES
- $('#idealist-item-'+$(this).attr('id')+' img').draggable({helper:"clone",opacity:".5"});
-
- // SET UP DROPPABLE AREAS
- var options = {};
- options.accept = '.idea-image';
- options.drop = function(event,info){ this.src = $(info.draggable).attr('src');};
-
- $('#grid-tablecloth').droppable(options);
- $('#grid-overlay').droppable(options);
- $('#grid-runner').droppable(options);
- $('#grid-napkin').droppable(options);
- $('#grid-chaircover').droppable(options);
- $('#grid-chairaccessory').droppable(options);
- });
-
- // Close the dialog box
- $("#loadidealist").dialog("close");
- }
- // Cancel button function
- var cancel = function() {
- //close the dialog
- $("#loadidealist").dialog("close");
- }
-
- // Set the dialog options including the button functions for Load and Cancel.
- var dialogOpts = {
- resizable: false,
- autoOpen: false,
- modal: true,
- zIndex: 9999,
- position: ['center','center'],
- buttons: {
- "Load": execute,
- "Cancel": cancel
- }
- };
-
- // The autoOpen option is false, the widget is created but not displayed.
- $("#loadidealist").dialog(dialogOpts);
-
- // The user must be logged in to be able to load an idea list.
- $("#load-ideas").click(function() {
- // If the user is not logged in the Load button will have a class of disabled.
- if($(this).hasClass('disabled')) {
- // Display an error message
- $('#thanks').remove();
- $('.list-message').addClass('idealisterror');
- $('<p id="thanks">').text('You must be logged in.').appendTo($('.list-message'));
- } else {
- // Toggle the dialog box
- ($("#loadidealist").dialog("isOpen") == false) ? $("#loadidealist").dialog("open")
- : $("#loadidealist").dialog("close");
- }
- });
- });