Adding textbox on button click with javascript

前端 未结 2 887
时光说笑
时光说笑 2021-01-16 22:15

I\'m working on a web form with a textbox for pets and an \"add pet\" button. Each time the button is clicked, an additional textbox should be displayed below the original o

2条回答
  •  遥遥无期
    2021-01-16 22:43

    You could easily add elements to the DOM:

    function createPetField() {
      var input = document.createElement('input');
      input.type = 'text';
      input.name = 'pet[]';
      return input;
    }
    
    var form = document.getElementById('myForm');
    document.getElementById('addPet').addEventListener('click', function(e) {
      form.appendChild(createPetField());
    });
    

提交回复
热议问题