0

So I have a js function :

function addNewUpload()
{
    if(formNumber == 1){
        displayRemove();
    }

    if(formNumber != 10){
        var html = " <div id='u_"+(formNumber+1)+"' > ";
        //html += " description : <input type='text' name='desc[]' /> <br> ";
        html += " photo : <input type='file' name='file[]' />   ";
        html += " </div> ";
        document.getElementById('uploadHolder').innerHTML += html;

        formNumber = formNumber + 1;
    } else {
        alert("You can only upload 10 photos.");
    }                                                                               
}

it works fine except that it removes the values from the fields that were already added using this function. How can I stop it from doing so?

Matt
  • 41,216
  • 30
  • 109
  • 147
Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65

2 Answers2

4

You should be using DOM methods like appendChild and createNewElement etc.

Try something along these lines:

http://jsfiddle.net/9YHs8/ (in this example, "file" for type was changed to text for demo purposes).

function addNewUpload()
{
    if(formNumber == 1){
        displayRemove();
    }

    if(formNumber != 10){
        var d = document.createElement("div");
        d.id = "u_" + formNumber;
        var i = document.createElement("input");
        i.type = "file";
        i.name = "file[]";
        d.appendChild(document.createTextNode(" photo : "));
        d.appendChild(i);
        document.getElementById('uploadHolder').appendChild(d);

        formNumber++;
    } else {
        alert("You can only upload 10 photos.");
    }                                                                               
}
Rob W
  • 341,306
  • 83
  • 791
  • 678
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
0

It's normal

You say "take the innerhtml of my block add some content to it and then reinitialize the inner html of my block with this string"

The form's value are not in the innerhtml !

Look at this thread : Inserting HTML elements with JavaScript

Community
  • 1
  • 1
remi bourgarel
  • 9,231
  • 4
  • 40
  • 73