Skip to content

Instantly share code, notes, and snippets.

@andreaseger
Created July 19, 2011 13:54
Show Gist options
  • Save andreaseger/1092429 to your computer and use it in GitHub Desktop.
Save andreaseger/1092429 to your computer and use it in GitHub Desktop.
dynamically add and remove dom elements(with js)
<div class="bodyClip" >
<div id="moreUploads">
<div id="att1">
<input type="file" name="appdocs_services[attachment1]" id="appdocs_services[attachment1]" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
</div>
</div>
<div id="moreUploadsLink" style="display:none;"><a href="javascript:addFileInput();">Eine weitere Datei hinzufügen.</a></div>
<p><i>(Format erlaubt : PDF, doc)</i></p>
</div>
var upload_number = 2;
function addFileInput() {
var d = document.createElement("div");
d.setAttribute("id","att"+upload_number);
var file = document.createElement("input");
file.setAttribute("type", "file");
file.setAttribute("name", "appdocs_services[attachment"+upload_number+"]");
var link = document.createElement("a");
link.setAttribute("href","javascript:removeFileInput(" + upload_number + ");");
var linkText = document.createTextNode("entfernen");
link.appendChild(linkText);
d.appendChild(file);
d.appendChild(link);
document.getElementById("moreUploads").appendChild(d);
upload_number++;
};
function removeFileInput(i) {
var div = "att"+i;
var d = document.getElementById("moreUploads");
var olddiv = document.getElementById(div);
d.removeChild(olddiv);
};
@chayouncheul
Copy link

dssdsdsd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment