Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create file input element

I want to customize the file input button, so I use this code to create an 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 create, it shows nothing.

like image 280
panda Avatar asked May 17 '12 21:05

panda


1 Answers

You're creating the new DOM element, but you're not attaching it to the DOM. You need something like:

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

You can see how this works in a poorly done JSFiddle here: http://jsfiddle.net/JQHPV/2/

like image 70
Marc Avatar answered Oct 15 '22 22:10

Marc