Execute javascript when editing an event.
Hello,
I'm working on the Events module and have two custom fields:
1. Checkbox (Type: Yes/No)
2. Textbox (Type: Short text)
My requirement is
- when the checkbox value is Yes, the textbox field should be shown.
- when the checkbox value is No, the textbox field should be hidden.
I'm thinking to use javascript to handle this but no idea to get started. Or is there a better way to accomplish this?
Thanks,
Brew
Hello Brew,
You can refer to the following blog post where the concept of extension scripts in Sitefinity is explained: http://www.sitefinity.com/blogs/vassil-vassilev-s-blog/vassil-vassilev's-posts/vassil-vassilevs-blog/2014/12/15/how-to-add-predefined-values-to-the-backend-create-view-of-dynamic-content-items
Once familiar with the concept here is an example extension script that would achieve your needs:
function OnDetailViewLoaded(sender, args) sender.add_formCreated(formCreatedHandler);function formCreatedHandler(sender, args) var detailFormView = sender, fieldControlIds = detailFormView._fieldControlIds, textBox; for (var i = 0, length = fieldControlIds.length; i < length; i++) var control = $find(fieldControlIds[i]); if (control) switch (control._fieldName) case "CheckboxFieldName": $(control._element).unbind('change'); $(control._element).on('change', function (element) if (element.target.checked) textBox.show(); else textBox.hide(); ); break; case "TextBoxFieldName": textBox = $(control._element); break; Hi Velizar,
This works fine when adding a new one. How about editing? How do I retrieve the value for the checkbox so I can set the textbox visibility when loading a page?
Ex.
Add mode: the checkbox field is checked; the textbox field is hidden. (this works ok with your suggestion)
Edit Mode: How to know the value of checkbox here to set the textbox visibility?
Thanks,
Brew