How to make onkeypress function in dynamic field

0

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

The central problem is: I can not get the setAtribute of the generated field to insert the function I created. Here is the code:

Search function:

 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 here 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++;
}
    
asked by anonymous 27.08.2015 / 20:50

1 answer

0

Try this:

input[cont].onkeypress = enter;

Fiddle

Or with addEventListener :

input[cont].addEventListener("keypress", enter);

Fiddle

    
27.08.2015 / 21:01