I would need to delete or leave the empty texts of a DIV or a P when pressing a delete button.
In the following code, what it does is from a SELECT I bring data from a database, and when selecting a surname it shows me data in ones <p>
with its ID , and when I press the delete button it deletes what I selected in SELECT perfectly , but at select another to delete DOES NOT DELETE ME , the previous data remains.
What will be wrong?
code in js
function EliminarSeleccionado() {
//capta lo que selecciono
var select = document.getElementById('buscarporapellido');
//boton Eliminar
var delButton = document.getElementById('btnEliminar');
function remove() {
value = select.selectedIndex;
select.removeChild(select[value]);
}
//remueve lo seleccionado dentro del SELECT
delButton.onclick = remove;
//vacia el contenido de los <p>
$("#Principalfide").empty();
$('#PrincipalLetra').empty();
}
Although you have not put all the code, I think I have already seen what happens to you. You put this like this:
therefore you are loading what that onclick contains (which will surely be a call to
EliminarSeleccionado()
) and the second time you do it, well, that is gone.Apart from that, you don't call
remove
correctly, it must be with()
at the end since it is a function.In short, this should work best for you:
but I don't see the point of putting it inside a function right now either, because you can easily leave it like this I suppose: