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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Widgets /

Discuss SilverStripe Widgets.

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

Load JS by adding widget


Go to End


1591 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?