Over a million developers have joined DZone.

Add A Div/HTML Tag On Mouse Click.

·
// description of your code here
The below mention function will be use to insert a div tag along with input field, button when a user clicks on hyper link. Button will be used to hide the selected div. A user can create max 5 div.


 function attachFile(id)
{
 var count = new Array("1","2","3","4","5")
 var hiddenCount = document.getElementById("Cnt").value;
 var Comman = 1;
 
 
 for(var j=1;j<=count.length;j++)
 {
     if (id == 'remove_' + j)
      {
         document.getElementById('div_' + j).style.display = "none";
         return
     } 
 }
 Comman = parseInt(hiddenCount) + Comman;
 if (Comman <= 5)
     {
        var varTag1 = document.createElement("INPUT");
       // var varTagRemove = newElement("input",{type:"button",name:"Remove",id:"remove_"+Comman,value:"1"},{onclick:attachFile(this.id)});
        var varTagRemove = document.createElement("INPUT");
        varTagRemove.type="button";
        varTagRemove.value = "Remove";
        varTagRemove.id = "remove_"+Comman;
        varTag1.type = "file";
        var x = document.all;
        var br = "br"
        if(x)
            br = "
" var varTag2 = document.createElement(br); document.getElementById("div_" + Comman).appendChild(varTag1); document.getElementById("div_" + Comman).appendChild(varTagRemove); document.getElementById("div_" + Comman).appendChild(varTag2); var rm_test = document.getElementById("remove_" + Comman); rm_test.onclick = function(){attachFile(this.id)}; document.getElementById('Cnt').value = Comman; } else { for (var i = 1; i <= count.length; i++) { if (document.getElementById("div_" + i).style.display == "none") { document.getElementById("div_" + i).style.display = "block"; return } } //ALERT WILL BE CALLED WHEN THE COUNT OF FILE UPLOADER IS 5 AND ALL THE DIV DISPLAY PROPERTY IS // BLOCK alert("Limit is over"); } }
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}