Hey Everyone,
what i am trying to do is everytime a file is uploaded i want a remove button to appear next to it. This works fine.
However, the problem i am having is every time i hover over the remove button you get the following names for example if i have 3 files the first file with a remove will say javascript:removeFileInput(f1), the second javascript:removeFileInput(f2),and 3rd javascript:removeFileInput(f3).
The problem with this is if i remove the second file the first one will still say f1, but the problem is with the 3rd one. The third one should change to f2 (since removing the second one makes the 3rd become the second one) but instead it still remains f3 and i am wondering how could i make it change to f2 ? the reason i need to be able to do this is because if i try to insert it into my database it gives me an error an asks for f2.
Here is the code i have
javascript
<script type="text/javascript">
var upload_number = 1;
function addFileInput()
{
var d = document.createElement("div");
var l = document.createElement("a");
var file = document.createElement("input");
var text = document.createElement("input");
d.setAttribute("id", "f"+upload_number);
file.setAttribute("type", "file");
file.setAttribute("name", "attachment"+upload_number);
text.setAttribute("type", "text");
text.setAttribute("name", "description"+upload_number);
l.setAttribute("href", "javascript:removeFileInput('f"+upload_n...
l.appendChild(document.createTextNode("R...
d.setAttribute("id", "f"+upload_number);
d.appendChild(file);
d.appendChild(text);
d.appendChild(l);
document.getElementById("moreUploads").
appendChild(d);
document.getElementById("totalAttachment... = upload_number;
upload_number++;
}
function removeFileInput(i)
{
var elm = document.getElementById(i);
document.getElementById("moreUploads").
removeChild(elm);
upload_number = upload_number - 1
}
</script>
form
<input type="file" name="attachment1" id="attachments"
value="" onchange="document.getElementById('moreU...
style.display = 'block';" />
Description <input type="text" name="description1
" id="description" value="" />
<div id="moreUploads"></div>
<div id="moreUploadsLink" style="display:none;">
<input type="button" value="Attach another file"
onclick="javascript:addFileInput();" >
</div>
<input type="hidden" id="totalAttachments" name="totalAttachments" value="1">
Thank you in advance,
Rach
Tags: