Jump to:

794 Posts in 409 Topics by 304 members


SilverStripe Forums » Widgets » Load JS by adding widget

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 893 Views
  • m-phil
    Community Member
    37 Posts

    Load JS by adding widget Link to this post

    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() {   

       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);

    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?

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.