function html2json() {
    // console.clear();

    $.fn.getTextQuestion = function(element_id) {
        var questionTitle = document.getElementById('textQuestionName_' + element_id).querySelector('.ql-editor').innerHTML;
        var isMathEnabled = document.getElementById('math_' + element_id).checked;
        var questionSubTitle = "";
        if(isMathEnabled == true)
        {
            questionSubTitle = document.getElementById('textQuestionSubtitle_math_' + element_id).value;
        }
        else
        {
            questionSubTitle = document.getElementById('textQuestionSubtitle_' + element_id).querySelector('.ql-editor').innerHTML;
        }
        var questionType = document.getElementById('question_type_' + element_id).value;
        var questionSubType = document.getElementById('longAnswer_' + element_id).checked;

        return {
            "question": questionTitle,
            "question_description": questionSubTitle,
            "question_type": questionType,
            "question_sub_type": questionSubType,
            "is_math_enabled": isMathEnabled
        }
    }

    $.fn.getChoiceQuestion = function(element_id) {

        var questionTitle = document.getElementById('textQuestionName_' + element_id).querySelector('.ql-editor').innerHTML;
        var isMathEnabled = document.getElementById('math_' + element_id).checked;
        var questionSubTitle = "";
        if(isMathEnabled == true)
        {
            questionSubTitle = document.getElementById('textQuestionSubtitle_math_' + element_id).value;
        }
        else
        {
            questionSubTitle = document.getElementById('textQuestionSubtitle_' + element_id).querySelector('.ql-editor').innerHTML;
        }
        var questionType = document.getElementById('question_type_' + element_id).value;
        var questionSubType = document.getElementById('multipleAnswer_' + element_id).checked;
        var answers = [];

        //get choice group

        //get default choice details
        var choice_name = "";
        if(isMathEnabled == true)
        {
            choice_name = document.getElementById('choiceTextField_math_' + element_id).value;
        }
        else
        {
            choice_name = document.getElementById('choiceTextField_' + element_id).querySelector('.ql-editor').innerHTML;
        }
        var choice_description = document.getElementById('commentField_' + element_id).value;
        // var choice_image = document.getElementById('imageInput_' + element_id).value;
        answers.push({
            "choice_name": choice_name,
            "choice_description": choice_description
        });

        // console.log('before group');
        //get dynamically added choice detailscolor: #999;
        var $newlyAddedChoices = $('#addChoiceArea_' + element_id);

        $newlyAddedChoices.children().each(function(index, element) {

            var groupChoiceId = ((element.id).split('_').slice(-1)[0]).split('.').slice(0)[0];
            var individualChoiceId = (element.id).split('.').slice(-1)[0];
            // console.log('newChoiceTextField_'+groupChoiceId+'.'+individualChoiceId);
            var choice_name = "";
            if(isMathEnabled == true)
            {
                choice_name = document.getElementById('newchoiceTextField_math_' + groupChoiceId + '.' + individualChoiceId).value;
            }
            else
            {
                choice_name = document.getElementById('newChoiceTextField_' + groupChoiceId + '.' + individualChoiceId).querySelector('.ql-editor').innerHTML;
            }
            // console.log('choice_description id in the html to json script');
            var id = document.getElementById('newChoiceCommentField_' + groupChoiceId + '.' + individualChoiceId);
            // console.log(id);
            var choice_description = document.getElementById('newChoiceCommentField_' + groupChoiceId + '.' + individualChoiceId).value;

            
            // var choice_image = document.getElementById('newChoiceImageInput_' + groupChoiceId + '.' + individualChoiceId).value;

            answers.push({
                "choice_name": choice_name,
                "choice_description": choice_description,
            });


        });


        return {
            "question": questionTitle,
            "question_description": questionSubTitle,
            "question_type": questionType,
            "question_sub_type": questionSubType,
            "is_math_enabled": isMathEnabled,
            "answers": answers
        }
    }


    $.fn.getQuestionsData = function(element_id, type = 'question') // type for single question or group question 
    {
        // console.log('getQuestionsData - '+element_id);

        var $questionArea = (type == 'question' ? $('#question_area_' + element_id) : $('#group_question_area_' + element_id));
        // console.log('getQuestionsData');
        // console.log($questionArea);

        var questionsArr = [];
        $questionArea.children("div").each(function(index, element) {

            var questionRowID = (element.id).split("_").slice(-1)[0];
            // console.log('row question' + questionRowID);
            var question_type = document.getElementById('question_type_' + questionRowID);
            // check whether current element is single question or question group
            if (typeof question_type !== null && question_type != undefined) {
                if (document.getElementById('question_type_' + questionRowID).value == 'text') {
                    //get text type question data
                    var textQuestion = $.fn.getTextQuestion(questionRowID);
                    questionsArr.push(textQuestion);

                } else {
                    // console.log('get choice question');
                    //get choice type question data
                    var getChoiceQuestion = $.fn.getChoiceQuestion(questionRowID);
                    questionsArr.push(getChoiceQuestion)
                }
            } else // handle question group
            {
                // console.log('QUESTION GROUP');
                // console.log('row question' + questionRowID);
                var questionGroupData = $.fn.getQuestionQroupData(element);
                questionsArr.push(questionGroupData)

            }


        });

        return questionsArr
    }

    $.fn.getQuestionQroupData = function(element) {
        // console.log('getSectionData');
        // console.log(element.id);
        const rand_id = (element.id).split("_").slice(-1)[0];
        // console.log(rand_id);
        // console.log('section_title_text_'+rand_id);
        var groupQuestion = {}
        var group_title = document.getElementById('subSection_title_text_' + rand_id);
        var group_description = document.getElementById('subSection_description_text_' + rand_id);
        groupQuestion['group_title'] = ((typeof group_title !== null && group_title !== 'undefined') ? (group_title !== null ? group_title.querySelector('.ql-editor').innerHTML : null) : null);
        groupQuestion['group_description'] = ((typeof group_description !== null && group_description !== 'undefined') ? (group_description !== null ? group_description.querySelector('.ql-editor').innerHTML : null) : null);

        if (group_title !== null) {
            var questionsData = $.fn.getQuestionsData(rand_id, 'group');
            // console.log('Section Data');
            // console.log(questionsData);
            groupQuestion['questions'] = questionsData;
            return groupQuestion;
        }

    }


    $.fn.getSectionData = function(element) {
        // console.log('getSectionData');
        // console.log(element.id);
        const rand_id = (element.id).split("_").slice(-1)[0];
        // console.log(rand_id);
        // console.log('section_title_text_'+rand_id);
        var section = {}
        var section_title = document.getElementById('section_title_text_' + rand_id);
        var section_description = document.getElementById('section_description_text_' + rand_id);
        section['section_title'] = ((typeof section_title !== null && section_title !== 'undefined') ? (section_title !== null ? section_title.querySelector('.ql-editor').innerHTML : null) : null);
        section['section_description'] = ((typeof section_description !== null && section_description !== 'undefined') ? (section_description !== null ? section_description.querySelector('.ql-editor').innerHTML : null) : null);

        if (section_title !== null) {
            var questionsData = $.fn.getQuestionsData(rand_id);
            // console.log('Section Data');
            // console.log(questionsData);
            section['questions'] = questionsData;
            return section;
        }

    }

    var formData = {};

    var question_paper_title = document.getElementById('Question_title').querySelector('.ql-editor').innerHTML;
    var question_paper_description = document.getElementById('Question_description').querySelector('.ql-editor').innerHTML;
    formData['question_paper_title'] = question_paper_title;
    formData['question_paper_description'] = question_paper_description;
    formData['sections'] = [];
    // console.log(formData);
    // return False;

    var $parentSectionElement = $("#addNewSection");
    $parentSectionElement.find(".section").each(function(index, element) {
        // console.log(element);
        //	if(index == 2){return false;}
        var sectionData = $.fn.getSectionData(element);
        // console.log(sectionData);
        if (sectionData !== null && sectionData !== undefined) {
            formData['sections'].push(sectionData);
        }

    });

    // console.log((formData));
    // console.log(JSON.stringify(formData, null, 1));
    localStorage.setItem("json", JSON.stringify((formData)));
    console.log(JSON.stringify(formData));
    // console.log(OBJtoXML(({'question_paper' : formData})));
}
