Question T8729
Visible to All Users

creator.onQuestionAdded

created 3 years ago

Hello,

Is there a way to call creator.onQuestionAdded from a button click event?

Example,

const handleOnDuplicateDeleteClick = React.useCallback((): void => { creator.onQuestionAdded.add(function (sender, options) { if (!!options.question.customname) { //Please make sure it is unique options.question.name = options.question.customname; } }); }, []);

Thanks

Answers approved by surveyjs Support

created 3 years ago

Hello Jarrad,
If you are calling adding new question from your code, then you can get a new question instance and do what you need to do.
If you describe your scenario, I am sure, we will find a solution, probably implement something if your scenario is a valid one.

Thank you,
Andrew
SurveyJS Team

    Comments (2)

      Hello Andrew,

      What we want to do is make a custom duplicate and delete button outside of the survey on our project so was wondering if we could call the creator.onQuestionAdded inside an onClick event

        Hello Jarrad,
        Here is the working example.

        JavaScript
        class SurveyComponent extends Component { constructor() { super(); this.state = { canCopyQuestion: false }; // Show Designer, Test Survey, JSON Editor and additionally Logic tabs var options = { showLogicTab: true }; //create the SurveyJS Creator and render it in div with id equals to "creatorElement" this.creator = new SurveyCreator.SurveyCreator("creatorElement", options); this.creator.onSelectedElementChanged.add((sender, options) => { const el = this.creator.selectedElement; this.setState({ canCopyQuestion: !!el && el.isQuestion }); }); this.creator.onQuestionAdded.add((sender, options) => { alert(options.question.name); }); this.duplicateQuestion = this.duplicateQuestion.bind(this); } duplicateQuestion() { this.creator.fastCopyQuestion(this.creator.selectedElement); } componentDidMount() {} render() { const canCopyQuestion = this.state.canCopyQuestion; return ( <> <button disabled={!canCopyQuestion} onClick={this.duplicateQuestion}> Duplicate </button> <SurveyCreator.SurveyCreatorComponent creator={this.creator} /> </> ); } }

        Thank you,
        Andrew
        SurveyJS Team

        Other Answers

        created 3 years ago

        Thanks Andrew that helps a lot,

        but would it be possible to have the button outside of the SurveyCreatorComponent

          Show previous comments (10)

            Yes, you can get question by name, check if the instance is not null or undefined and call delete() function.

            Thank you,
            Andrew
            SurveyJS Team

              So el.delete is giving me the following error,

              Property 'delete' does not exist on type 'Base'

                Question class has delete() function. If you instance is a question then delete() function will work as in my example.
                You can check it as: yourInstance.isQuestion it should return true if an object is a question.

                Thank you,
                Andrew
                SurveyJS Team