1

I have written a JQuery plugin for some content which is tabbed content on desktop devices and an accordion on mobile devices. I've used the JQuery Boilerplate (https://github.com/jquery-boilerplate/jquery-boilerplate/blob/master/src/jquery.boilerplate.js) as a start point pattern for the plugin file.

I now need to use this plugin on the site within an ajax-loaded modal window. The HTML for the plugin is loading into the modal window fine but the javascript for the plugin is not running.

I believe I need to trigger the init function of my plugin from within the jQuery .load() function which loads the content of the modal window. The function for the ajax overlay looks like this:

    menuOverlays = (function(){
        var toggleMenuOverlay = function(){
            //Toggle a class on the body which triggers styles for the overlay's appearance
            $("body").toggleClass('overlay');

            //Set the overlay expected to active
            var $activeOverlay = $("#"+$(this).data("overlay-element"));
            $activeOverlay.toggleClass('active');

            //If there is a href on the link that the user clicked on, grab the URL from the link and load the content from that URL into the modal
            if($(this).attr("href")){
                var urlToLoad = $(this).attr("href") + " #ajaxContent";
                $activeOverlay.find(".ajax-content").empty().load(urlToLoad);
            }

            return false;
        };

        return{
            init: function(){
                $("[data-overlay-element]").on("click", toggleMenuOverlay);
            }
        };
    }()),

I have read the following on how to trigger JQuery Boilerplate plugin methods:

Call methods using jQuery Plugin design pattern

and thus tried this:

$activeOverlay.find(".ajax-content").empty().load(urlToLoad, function({
    var $t2a = $('.tabs2accordion').tabs2Accordion().data('plugin_tabs2Accordion');
    $t2a.init();
});

But I get:

Uncaught TypeError: Cannot read property 'data' of undefined

Any ideas?

Community
  • 1
  • 1
James Howell
  • 1,392
  • 5
  • 24
  • 42

0 Answers0