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, Ed, biapar, Willr, Ingo, swaiba

Load JS by adding widget

Go to End



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

	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?