1

I have an EditForm that I am using JSLink to run some javascript that gets the Content Type and then sets a field according to the Content Type.

This works fine when the form loads, the trouble is when the user changes the Content Type the javascript does not run. Is there a way to get the javascript to run when the Content Type dropdown is changed?

1

Make sure the custom template is registered after the clienttemplates.js library is loaded.

The following example prints into console:

PreRender event is triggered
PostRender event is triggered

when the edit form is loaded and once content type is changed (ContentTypeChoice control)

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides({

      OnPreRender: function(ctx) {
          console.log('PreRender event is triggered');
      },


      OnPostRender: function(ctx) {
         console.log('PostRender event is triggered');
      }

    });

});
2

I believe that, when you change a value in the Content Type dropdown, the form is submitted and loaded again, so your CSR code should work.

On the other hand, you can try to use JQuery and bind you functions to the change() event on the HTML select element. You can select Content Type dropdown using $('select[id$="_ContentTypeChoice"] selector.

  • Thanks, yes I thought the page gets resubmitted on the dropdown change but unfortunately the code does not run. I haven't used JQuery before, would I need to put a CEWP on the page to get this to work? – Mat May 6 '15 at 11:15
  • 1
    You can add JQuery the same way you are adding JSLink. For example, in the edit form properties with | as separator. But if you code does not works after the resubmission maybe your function runs two early. Look at the @vadim answer and call your code in the PostRender event. – ECM4D May 6 '15 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.