How to do onkeypress function in dynamic field

Asked

Viewed 523 times

0

I have a search screen where there are several checkboxes that, when selected, display dynamically generated fields. I would like to put a "onkeypress" function so that when the enter button is pressed the search function is performed.

The central problem is: I can’t get the generated field setAtribute to enter the function I created. Below follows the code:

Function that does the search:

 function enter(e)
 {
     var unicode =e.keyCode? e.keyCode : e.charCode
     if(unicode == 13)
     {
        pesquisa();
     }
 }

Onkeypress that should enter the dynamic field:

onkeypress="enter(event)"

And this is where the field is generated dynamically:

if(document.getElementById(sID).checked == true)
{
    var table = document.createElement("table");
    var input = new Array();
    var p    = new Array();
    var tr  = new Array();
    var td  = new Array();

    var camada = document.getElementById("campos");

    p[cont]     = document.createElement("p");
    tr[cont]    = document.createElement("tr");
    td[cont]    = document.createElement("td");
    input[cont] = document.createElement("input");

    table.border = 0;
    table.width  = "350px";
    table.id     = 'tbl_'+sID;
    td[cont].height = "35px";

    input[cont].id   = 'inp_'+sID;
    input[cont].name = 'inp_'+sID;
    input[cont].type = 'text';
    input[cont].setAttribute("style","width:500px;");

    var label = document.createTextNode(sValue+':');

    p[cont].appendChild(label);
    td[cont].appendChild(p[cont]);
    td[cont].appendChild(input[cont]);
    tr[cont].appendChild(td[cont]);
    table.appendChild(tr[cont]);
    camada.appendChild(table);
    cont++;
}
  • thought about input[cont].addEventListener("keypress", enter)?

1 answer

0


  • That’s how it worked, thank you very much!

Browser other questions tagged

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