Как я могу исправить ошибку removeLastQuestion ()? - PullRequest
0 голосов
/ 16 февраля 2019

Я новичок в JS и пытаюсь удалить последний элемент div, созданный мной с помощью JS.Но всегда есть эта ошибка: Не удалось выполнить «removeChild» на «Узле»: параметр 1 не относится к типу «Узел». Дело в том, что если я ввожу количество вопросов меньше первого введенного числа, я удаляю количество вопросовбольше последнего

1002 *
<span>How many questions you want to add?</span>
<input type="text" id="qNum" >
<div id="containerBlocQ">`</div>
<button onclick="fieldAdd()">Add fields</button>
<button onclick="addNewQues()">Add a questions</button>
<button onclick="removeLastQuestion()">Remove a questions</button>

function fieldAdd(){
        let qNumber = document.getElementById("qNum").value;
        let containerBlocQ=document.getElementById('containerBlocQ');
        totalNumOfQuest = qNumber - nbQst;
        if(qNumber<=0){
            totalNumOfQuest=0;
        }
        if(totalNumOfQuest>=0){
            for(let i=1; i <= totalNumOfQuest ; i++){
                 addNewQues();
             }

        }
        else{
            for(i=0;Math.abs(totalNumOfQuest);i++){
                //Here is the error
                removeLastQuestion();
                nbQst--;
            }
        }


    }
    function removeLastQuestion(){
        nbQst--;
        let containerBlocQ = document.getElementById('containerBlocQ');
        containerBlocQ.removeChild(containerBlocQ.lastChild);
    }
...