Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Widgets /

Discuss SilverStripe Widgets.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Load JS by adding widget


Reply


909 Views

Avatar
m-phil

Community Member, 37 Posts

30 August 2010 at 6:35pm

Hello there,
I use a customized PageContent widget. My ambition is to fill the title field by choosing a page in the dropdown. For this a have a little js function called dynamicTitleHandler():

;(function($) {
   $(document).ready(function() {   
      dynamicTitleHandler();
   });

   function dynamicTitleHandler() {
      
      // Add widget click handler for dynamic title set
      if ($('#usedWidgets-Sidebar').length) {
         $('#usedWidgets-Sidebar .PageContentWidget select:first-child option').click(function() {
            var title = $(this).text();
            $(this).closest('.widgetFields').find('.field.text input').val(title);
         });
      }
   }
})(jQuery);

I call it via Requirements::javascript(PAGECONTENT_DIR.'/javascript/pagecontent.js'); in getCMSFields().
It works fine, but only for the existing widgets. If I add a new one I have to call the function, but I don't know how. First I thought about adding a LiteralField to the widget form which calls it by onload.

new LiteralField('JSLoader', "<div onload='dynamicTitleHandler()'></div>")


but it isn't shown?
Any ideas, how to get it?