Onclick calling function javascript deletes textbox htm

Asked

Viewed 322 times

2

I have a text control HTML which is created by a function called OnClientClick button. The problem that every time I add a new Textbox, the previous data are deleted.

What to do to keep him from going out?

Note: As I am creating via HTML, I can’t create asp:UpdatePanel!

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;
    }
  • post the other relevant parts of the code please

  • It seems to me that the problem may be in the function valueDoCampo(), called in the input onBlur. If you can post function, it is easier to say.

  • Since Voce is using Asp.net, after running its functionVoce needs to give a false Return in Onclientclick, if there will be a post in the form. As this value does not exist in the Viewstate page is deleted.

2 answers

0

Dude, if you use Jquery, you can use code this way:

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;
    }

0

the innerHTML is what is erasing the contents of Textbox previous. 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.