I can't insert the indexOf function in the click event of a listener. It shows me the following error:
Uncaught TypeError: v.indexOf is not a function
The code where is the error:
function mostrar_datos(resp) {
let tabla = document.getElementsByClassName("table");
let fila = '';
let curso = '';
let version = '';
let programa = '';
let accion = '';
let anyadir = '';
let editar = '';
let borrar = '';
const indice = 0;
for(let i = 0; i < resp.length; i++) {
fila = tabla[0].insertRow(1);
curso = fila.insertCell(0);
version = fila.insertCell(1);
programa = fila.insertCell(2);
accion = fila.insertCell(3);
curso.innerHTML = resp[i].nombre_c;
version.innerHTML = resp[i].version;
programa.innerHTML = resp[i].contenido;
accion.innerHTML = '<a class="add" title="" data-toggle="tooltip"><i class="fas fa-plus"></i><a class="edit" title="" data-toggle="tooltip"><i class="fas fa-pen"></i><a class="delete" color="blue" title="" data-toggle="tooltip"><i class="fas fa-trash"></i>';
document.getElementsByClassName("edit")[0].addEventListener("click", function() {
t = document.querySelector(".table");
v = t.getElementsByTagName('td');
indice = v.indexOf(resp[i].nombre_c); // error indexOf is not a funtion
});
}
}
v is an HTMLCollection that is a simple index array. I thought that indexOf cannot be used in that case and I tried to change it for a function created by me in the following way:
function mostrar_datos(resp) {
let tabla = document.getElementsByClassName("table");
let fila = '';
let curso = '';
let version = '';
let programa = '';
let accion = '';
let anyadir = '';
let editar = '';
let borrar = '';
const indice = 0;
for(let i = 0; i < resp.length; i++) {
fila = tabla[0].insertRow(1);
curso = fila.insertCell(0);
version = fila.insertCell(1);
programa = fila.insertCell(2);
accion = fila.insertCell(3);
curso.innerHTML = resp[i].nombre_c;
version.innerHTML = resp[i].version;
programa.innerHTML = resp[i].contenido;
accion.innerHTML = '<a class="add" title="" data-toggle="tooltip"><i class="fas fa-plus"></i><a class="edit" title="" data-toggle="tooltip"><i class="fas fa-pen"></i><a class="delete" color="blue" title="" data-toggle="tooltip"><i class="fas fa-trash"></i>';
document.getElementsByClassName("edit")[0].addEventListener("click", function() {
t = document.querySelector(".table");
v = t.getElementsByTagName('td');
editar("curso"); // error editar is not a funtion
});
}
}
function editar() {
$(this).parents("tr").find("td:not(:last-child)").each(function(){
$(this).html('<input type="text" class="form-control" value="' + $(this).text() + '">');
});
$(this).parents("tr").find(".add, .edit").toggle();
$(".add-new").attr("disabled", "disabled");
}
Why I can't insert a function in this click event?
Thanks
nombre_cis a string, when you try to callyourElement.indexOf(nombre_c)you will keep getting an error. Instead you should callyourElement.innerText.indexOf(nombe_c)(oryourElement.innerHTML.indexOf(nombe_c))