How would I dynamically create input boxes on the fly?

后端 未结 4 800
走了就别回头了
走了就别回头了 2021-01-23 19:55

I want to use the value of a HTML dropdown box and create that number of input boxes underneath. I\'m hoping I can achieve this on the fly. Also if the value changes it should a

4条回答
  •  滥情空心
    2021-01-23 20:39

    Doing it in javascript is quite easy. Assuming you've got a number and an html element where to insert. You can obtain the parent html element by using document.getElementById or other similar methods. The method assumes the only children of the parentElement is going to be these input boxes. Here's some sample code:

    function addInput = function( number, parentElement ) {
        // clear all previous children
        parentElement.innerHtml = "";
        for (var i = 0; i < number; i++) {
            var inputEl = document.createElement('input');
            inputEl['type'] = 'text';
            // set other styles here
            parentElement.appendChild(inputEl);
        }
    }
    

    for the select change event, look here: javascript select input event

提交回复
热议问题