Onclick calling function javascript erases the textbox htm

2

I have a text HTML control that is created by a function called in the OnClientClick of the button. The problem is that every time I add a new Textbox , the old data goes out.

What do you do to prevent it from going out?

Javascript Function

function novo() {

        var form, quant;

        if (parseInt(i) < 51) {
            form = "<table width='96%' border='0'><tr><td width='8%' class='tblNormal' align='Right'>CPF:</td>"
            form = form + "<td width='12%' class='tblNormal'> <input type='text' onkeypress='FiltraTecla(event);' id='cpf" + i + "' value='' size='10' maxlength='10' onblur='valorDoCampo(this.value, " + i + ")'> "
            form = form + "</tr></table>"

            document.getElementById('<% =qtd.ClientID %>').value = i;

            i = i + 1;
            formularios.innerHTML = formularios.innerHTML + form + "<br>";

        }
        else {
            alert("Numero máximo excedido.");
        }
        return false;
    }
    
asked by anonymous 07.07.2015 / 14:46

2 answers

1

innerHTML is what is erasing the content of Textbox above. Instead of innerHTML , use appendChild :

function novo() {
    var form, quant;

    if (parseInt(i) < 51) {
        form = document.createElement('table')
        form.setAttribute('width', '96%');
        form.setAttribute('border', '0');
        form.innerHTML = "<tr><td width='8%' class='tblNormal' align='Right'>CPF:</td>";
        form.innerHTML+= "<td width='12%' class='tblNormal'> <input type='text' onkeypress='FiltraTecla(event);' id='cpf" + i + "' value='' size='10' maxlength='10' onblur='valorDoCampo(this.value, " + i + ")'> ";
        form.innerHTML+= "</tr>";

        document.getElementById('<% =qtd.ClientID %>').value = i;

        i = i + 1;
        formularios.appendChild(form); // adiciona o formulario
        formularios.appendchild(document.createElement('br')) // adiciona nova linha

    }
    else {
        alert("Numero máximo excedido.");
    }
    return false;
}
    
18.12.2016 / 05:15
0

Dude, if you use JQuery, you can use code like this:

function novo() {

        var form, quant;

        if (parseInt(i) < 51) {
            form = "<table width='96%' border='0'><tr><td width='8%' class='tblNormal' align='Right'>CPF:</td>"
            form = form + "<td width='12%' class='tblNormal'> <input type='text' onkeypress='FiltraTecla(event);' id='cpf" + i + "' value='' size='10' maxlength='10' onblur='valorDoCampo(this.value, " + i + ")'> "
            form = form + "</tr></table>"

            document.getElementById('<% =qtd.ClientID %>').value = i;

            i++;
            formularios.append(form);

        }
        else {
            alert("Numero máximo excedido.");
        }
        return false;
    }
    
07.07.2015 / 15:42