JavaScript

Moderators: None (Apply to moderate this forum)
Number of threads: 2061
Number of posts: 5164

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
renaming input elements Posted by yssirhc on 31 Mar 2009 at 10:05 AM
Does anyone know why, in my removeAdditionalFile function, the elements are not getting renamed? At least I think that's the problem. Although, if I use the remove link ONCE before uploading, it works ok. Using it TWICE before uploading is where the problem's at. Can anyone figure out what's wrong with my remove function?

function addFileTextbox()
{
  var divLocation = document.getElementById('addToDiv');
  var newDiv = document.createElement('div');
  var count = document.getElementById('addToDiv').getElementsByTagName('input').length + 2;
  var divIDName = 'file' + count;
  var removeLinkID = 'removeLink' + count;
  newDiv.innerHTML = '<input type="file" name="' + divIDName + '" id="' + divIDName + '" size="57"> <a href="#" id="' + removeLinkID + '" onclick="removeAdditionalFile(this.parentNode, ' + count + ')">Remove</a>';
  divLocation.appendChild(newDiv);

  var fileCount = document.getElementById('addToDiv').getElementsByTagName('input').length + 1;
  document.getElementById('fileCount').value = fileCount;
}


function removeAdditionalFile(divIDNode, count)
{
  var divLocation = document.getElementById('addToDiv');
  divLocation.removeChild(divIDNode);
  var fileCount = document.getElementById('addToDiv').getElementsByTagName('input').length + 1;
  document.getElementById('fileCount').value = fileCount;
        
  var nextFile = count + 1;
  while (document.getElementById('file' + nextFile)) //there are file textboxes after the one deleted
  {
    //rename the file textbox, its ID, & the remove link's ID to be one less & change the count parameter of the remove link
    document.getElementById('file' + nextFile).name = "file" + count;
    document.getElementById('file' + nextFile).id = "file" + count;
    document.getElementById('removeLink' + nextFile).onclick = function()
    {
      removeAdditionalFile(this.parentNode, ' + count + ');
    }
    document.getElementById('removeLink' + nextFile).id = "removeLink" + count;
    nextFile++;
    count++;
  }
}

Click Browse to select a file on your computer, or type its path in the box below.<br />To upload multiple files at once, click Add.<br />

<input type="button" name="add" value="Add Another File" onclick="addFileTextbox();">

<form method="POST" name="fileForm" enctype="multipart/form-data" action="repos.exe">
  File(s): <input type="file" size="66" name="file1" id="file1">
  <input type="hidden" name="fileCount" id="fileCount" value="1">
  <div id="addToDiv"></div> <!-- additional file textboxes will be added here -->
  <input type="submit" name="submit" value="Upload">
</form>




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.