Jump to content

Fields become empty when adding using innerHTML


laPistola

Recommended Posts

The JS in question

<script type="text/javascript">
<!--
function addField() {
  var txt = "<input name=\"uFile[]\" type=\"file\" id=\"uFile[]\" size=\"50\" /><br />";
  document.getElementById("uploadFields").innerHTML += txt;
}
//-->
</script>

 

The effecting element

<p id="uploadFields">
          <input name="uFile[]" type="file" id="uFile[]" size="50" /> <a href="javascript:addField();">Add another photo</a><br />
        </p>

 

This is a multiple file upload form but if i select a file and need to add another field for another file you click the link Add another photo, this is adding a new field to the list but its wiping out the contents in the other file fields??

 

Any incite would be great thank you!

Link to comment
Share on other sites

the function needs to "save" the values in the original fields and then, when it re-creates those fields when it updates the content of "uploadFields," it can set the VALUE parameter to that "saved" value.

 

I'd add something like this to your function:

 

<script type="text/javascript">
<!--
function addField() {

//get value of current field
var current_value = document.getElementByID['uFile'].value;

//create current field
var txt = "<input name=\"uFile[]\" type=\"file\" id=\"uFile[]\" size=\"50\" value=\""+ current_value +"\" /><br />";

//append with new blank field extra field  
txt += "<input name=\"uFile[]\" type=\"file\" id=\"uFile[]\" size=\"50\"/><br />";

 document.getElementById("uploadFields").innerHTML = txt;
}
//-->
</script>

 

of course, my example wont work after you'ved added 2 or more fields.  You'll need to loop through ALL of the existing fields since there can be more then one element named "uFile"

 

hope that helps a little

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.