Dynamically create a file entry element

I want to configure a file input button, so I use this code to create a file input element

function inputBtn(){ var input=document.createElement('input'); input.type="file"; setTimeout(function(){ $(input).click(); },200); } <button id="ifile" onclick="inputBtn()">create</button> 

However, when I click the create button, it shows nothing.

+6
source share
1 answer

A new DOM element is created, but you do not bind it to the DOM. You need something like:

 document.getElementById('target_div').appendChild(input); 

You can see how it works in poorly executed JSFiddle here: http://jsfiddle.net/JQHPV/2/

+8
source

Source: https://habr.com/ru/post/916042/


All Articles