Javascript Injection creates new fields

Have used many times JSI but now, with the following code, new duplicate fields are being created in the stream.
Fields “teclado” & “numerico” are declared before going into JSI but stream afterwards has also “teclado_1” & “numerico 1” with no data in it.
How can this be avoided, because later on in the process it reports index problems.

const a = document.getElementsByClassName(“boton_teclado_virtual”);
const n = document.getElementsByClassName(“boton_numpad_virtual”);
var teclado = ;
var numerico = ;
for (let j = 0; j < n.length; j++) {
numerico[j] = n[j].value;
}
for (let k = 0; k < a.length; k++) {
teclado[k] =a[k].value;
}
return “<<>>teclado::” + teclado +“<<>>;<<>>numerico::”+numerico+“<<>>”

1 Like

@wllmdjngh

In this modified code:

  1. The teclado and numerico arrays are initialized as empty arrays.
  2. The values are only added to the arrays if the corresponding element has a valid value.
  3. The join method is used to concatenate array elements into a string with comma as a separator.

This should help you avoid creating duplicate fields with empty values in the stream.

Code:
const a = document.getElementsByClassName(“boton_teclado_virtual”);
const n = document.getElementsByClassName(“boton_numpad_virtual”);
var teclado = ;
var numerico = ;

// Process “boton_teclado_virtual” elements
for (let k = 0; k < a.length; k++) {
// Check if the element has a valid value before adding it to the array
if (a[k].value) {
teclado.push(a[k].value);
}
}

// Process “boton_numpad_virtual” elements
for (let j = 0; j < n.length; j++) {
// Check if the element has a valid value before adding it to the array
if (n[j].value) {
numerico.push(n[j].value);
}
}

return “<<>>teclado::” + teclado.join(“,”) + “<<>>;<<>>numerico::” + numerico.join(“,”) + “<<>>”;

1 Like

Thanks Prajakta.
Let me review the code.
Regards.