Thursday, June 4, 2015

javascript- dynamically adding file upload fields to a form using javascript

<script language="javascript" type="text/javascript">
       var a=1;
        function AddMoreImages() {

         
            var fileUploadarea = document.getElementById("fileUploadarea");
          
            //var newLine = document.createElement("br");
            //fileUploadarea.appendChild(newLine);
            var newFile = document.createElement("input");
            newFile.type = "file";
          

      
             
            newFile.setAttribute("id", "FileUpload" + a);
            newFile.setAttribute("name", "file[]");
            newFile.setAttribute("class", "browse-snap");
            var div = document.createElement("div");
            div.appendChild(newFile);
            div.setAttribute("id", "div" + a );
            fileUploadarea.appendChild(div);
          
             var newbot= document.createElement("input");
                newbot.type="Button";
                newbot.setAttribute("id","b" + a);
                newbot.setAttribute("value","remove" + a);
                newbot.setAttribute("class","close-btn");
                newbot.setAttribute("onclick","deletefile(this.id);");
                var divid=document.getElementById("div" + a);
               fileUploadarea.appendChild(div).appendChild(newbot);
             
            a++;
            return false;
        }
      
        function deletefile(abf)
        {
        a--;
        var abf1 = abf.split("b");
        //var uplod=document.getElementById("fileUploadarea");
        var im=document.getElementById("div" + abf1[1]);
        im.parentNode.removeChild(im);
        return false;
      
        }
    </script>
<body>
    <form id="form1">
    <div>
        <div id="fileUploadarea">
          
        </div>
        &nbsp;
        <input style="display: block;" id="btnAddMoreImages" type="button" value="Add more images" onClick="AddMoreImages();" />
      
    </div>
    </form>
</body>
</html>

No comments:

Post a Comment