DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Add A Div/HTML Tag On Mouse Click.

12.24.2008
| 9991 views |
  • submit to reddit
        // 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 = "<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");
      }
  }