How to create <input type=“text”/> dynamically

后端 未结 12 1007
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 10:47

I want to create an input type text in my web form dynamically. More specifically, I have a textfield where the user enters the number of desired text fields; I want the tex

相关标签:
12条回答
  • 2020-12-02 11:25

    you can use ES6 back quits

      var inputs = [
            `<input type='checkbox' id='chbox0' onclick='checkboxChecked(this);'> <input  type='text' class='noteinputs0'id='note` + 0 + `' placeholder='task0'><button  id='notebtn0'                     >creat</button>`, `<input type='text' class='noteinputs' id='note` + 1 + `' placeholder='task1'><button  class='notebuttons' id='notebtn1' >creat</button>`, `<input type='text' class='noteinputs' id='note` + 2 + `' placeholder='task2'><button  class='notebuttons' id='notebtn2' >creat</button>`, `<input type='text' class='noteinputs' id='note` + 3 + `' placeholder='task3'><button  class='notebuttons' id='notebtn3' >creat</button>`, `<input type='text' class='noteinputs' id='note` + 4 + `' placeholder='task4'><button  class='notebuttons' id='notebtn4' >creat</button>`, `<input type='text' class='noteinputs' id='note` + 5 + `' placeholder='task5'><button  class='notebuttons' id='notebtn5' >creat</button>`, `<input type='text' class='noteinputs' id='note` + 6 + `' placeholder='task6'><button  class='notebuttons' id='notebtn6' >creat</button>`, `<input type='text' class='noteinputs' id='note` + 7 + `' placeholder='task7'><button  class='notebuttons' id='notebtn7' >creat</button>`, `<input type='text' class='noteinputs' id='note` + 8 + `' placeholder='task8'><button  class='notebuttons' id='notebtn8' >creat</button>`, `<input type='text' class='noteinputs' id='note` + 9 + `' placeholder='task9'><button  class='notebuttons' id='notebtn9' >creat</button>`
        ].sort().join(" ");
       document.querySelector('#hi').innerHTML += `<br>` +inputs;
    <div id="hi"></div>

    0 讨论(0)
  • 2020-12-02 11:25
    <button id="add" onclick="add()">Add Element</button>
    
    <div id="hostI"></div>
    
    <template id="templateInput">
        <input type="text">
    </template>
    
    <script>
        function add() {
    
            // Using Template, element is created
            var templateInput = document.querySelector('#templateInput');
            var clone = document.importNode(templateInput.content, true);
    
            // The Element is added to document
            var hostI = document.querySelector('#hostI');
            hostI.appendChild(clone);
        }
    
    </script>
    

    HTML Templates are now the recommended standards to generate dynamic content.

    0 讨论(0)
  • 2020-12-02 11:27
    • Query and get the container DOM element

    • Create new element

    • Put new element to document Tree

    //Query some Dib region you Created
    let container=document.querySelector("#CalculationInfo .row .t-Form-itemWrapper");
    let input = document.createElement("input");
    
    //create new Element  for apex
    input.setAttribute("type","text");
    input.setAttribute("size","30");
    containter.appendChild(input); // put it into the DOM
    
    0 讨论(0)
  • 2020-12-02 11:29

    You could use createElement() method for creating that textbox

    0 讨论(0)
  • 2020-12-02 11:33

    Using Javascript, all you need is document.createElement and setAttribute.

    var input = document.createElement("input");
    input.setAttribute('type', 'text');
    

    Then you can use appendChild to append the created element to the desired parent element.

    var parent = document.getElementById("parentDiv");
    parent.appendChild(input);
    
    0 讨论(0)
  • 2020-12-02 11:34

    Maybe the method document.createElement(); is what you're looking for.

    0 讨论(0)
提交回复
热议问题