Search code examples
jqueryajaxjquery-tabs

Load jQuery tabs from Ajax


I have a loading image that works using the code below. It shows the animated gif and waits for the html to be fetched from the url. Once it is fetched it hides the loading div and loads the html into the bite div. The html that is returned is all contained within jQuery Tabs however when the html is displayed the tabs are not rendered and just the li elements are shown.

Is it possible to fetch this html that contains jQuery tabs via AJAX and render these tabs? If so what am I doing wrong?

<div id="loader" style="text-align:center; display:none;">
<img src="img/ajax-loader.gif" alt="LOADING" />
</div>

<div id="bite"></div>

$(document).ready(function () {
  $('#loader').show();
  $.ajax({
    url:'http://www.domain.com/bitesized/main.php?uid=<?php echo $uid; ?>',
    complete: function(data){
        $('#loader').hide();
        $('#bite').html(data.responseText);
        // these divs ids are available after the load above and are meant to render the tabs
        $("#tabs").tabs();
        $("#fragment-a").tabs();
    }
  });
});

Solution

  • I created this code, which can load AJAX with a spinner. Note that i set and use the option "ajax_href", which come from html rel attribute. You can also set option directly from each link. I also use "width" and "height" to resize each tab-content. looks nice, when having different content or files for the tabs.

    $(document).ready(function() {
    
    // check
    $('.jquery_tabs_ajax').each(function() { 
    
        // tabs
        var $tabs = $(this).tabs({ 
    
            cache: false,
            ajaxOptions: { async: true, cache: false },
            show: function(event, ui) {
    
                // get
                var my_panel_id = '#ui-tab-' + (ui.index + 1);
                var get_panel_id = $(my_panel_id);
                var get_parent_link = get_panel_id.parents('ul:eq(0) li a[href="' + my_panel_id + '"]');
                var get_parent_rel = get_parent_link.attr('rel');
    
                // check options
                if(get_parent_rel) {
    
                    // option object
                    var $obj_option = {};
    
                    // split
                    $split_option_a = get_parent_rel.split(';');
                    for(var so = 0; so < $split_option_a.length; so++) {
                        $split_option_b = $split_option_a[so].split('=');
                        $obj_option[$split_option_b[0]] = $split_option_b[1];
                    }
    
                    // load AJAX
                    if($obj_option.ajax_href) {
    
                        // set spinner
                        get_panel_id.html('<div class="spinner_container"><div class="spinner"></div></div>')
    
                        // load AJAX
                        $.ajax({
    
                            type: "POST",
                            url: $obj_option.ajax_href,
                            async: true,
                            cache: false,
                            success: function(response_text) {
    
                                // set HTML
                                get_panel_id.html(response_text);
                            }
                        });
                    }
    
                    // check resize
                    if($.colorbox && ($obj_option.width || $obj_option.height)) {
    
                        // resize
                        $.colorbox.resize($obj_option);
                    }
                }
            }
        });
    });
    

    });