+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Execute javascript when editing an event.

Execute javascript when editing an event.

3 posts, 0 answered
  1. Brew Hutch
    Brew Hutch avatar
    12 posts
    Registered:
    15 Apr 2010
    18 Apr
    Link to this post

    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

  2. Velizar Bishurov
    Velizar Bishurov avatar
    143 posts
    Registered:
    23 Sep 2016
    21 Apr
    Link to this post
    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;
                        }
                }
            }
        }
    }

    Regards,
    Velizar Bishurov
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. Brew Hutch
    Brew Hutch avatar
    12 posts
    Registered:
    15 Apr 2010
    21 Apr in reply to Velizar Bishurov
    Link to this post

    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

3 posts, 0 answered