good morning. First of all I apologize for how unclear the title is, I have not been able to describe it better. Secondly, I clarify that what I am going to put here is an invented and simple example so that you understand what is my doubt and avoid complicating us unnecessarily by trying to resolve my doubt in my code.
The example is as follows: On the page there is an input where to write and a button. Pressing the button executes the following function: - Using an innerHTML, create an html element (a span) with the value of the input as id. -In addition I want you to create an addEventListener on that object, but of course, the id of that span I only have it stored in the variable and I do not know how to use the variable to refer to each of the span that are created.
document.addEventListener("DOMContentLoaded", function(){
var listaSpan = "";
document.getElementById("boton").addEventListener('click', function(){
valor = document.getElementById("cajaDeTexto").value;
listaSpan += "<span id='"+ valor +"'>"+ valor +"</span> ";
document.getElementById("lista").innerHTML = listaSpan;
//hasta aquí todo bien, es justo aquí donde insertaria una linea con la que me surge la duda. Debajo del ejemplo lo explico.
}, false);
}, false);
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
div{
padding: 20px;
}
span{
background-color: #8dc3ed;
padding: 10px;
display: block;
border-radius: 3px;
margin-bottom: 10px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ejemplo</title>
</head>
<body>
<div style="background-color:#d0d0d0">
Introducir nombre:
<input type="text" name="" value="" id="cajaDeTexto">
<button type="button" name="button" id="boton">Botón</button>
</div>
<div id="lista">
<span>Aquí irán apareciendo los elementos span a medida que sean introducidos mediante la caja de texto y el botón</span>
</div>
</body>
</html>
So far so good, if I inspect the page the element span has been created correctly in the DOM with its corresponding id, in fact you can see it in the example. But then, I want to add an addEventListener to each of the objects that are created but obviously I can not put something like this:
document.getElementById(valor).addEventListener("click", function(){
document.getElementById(valor).style.display = "none"; //por ejemplo
}, false)
//tambien he probado: .getElementById("valor") y .getElementById("'"+valor+"'")
What I would like is to be able to print the value of the variable value directly in the js code so that it can be interpreted that way. I do not know if I explain myself ... Maybe I'm confusing myself, I'm not sure what the problem is, let alone the solution. In short, what I want is that for each one of those spans that are created, an addEventListener is created on each one of them. Thank you very much for your time!
This is my code actually:
var lista = document.getElementById("lista");
function listaCookies(){ //actualiza la lista de cookies
var cookies = document.cookie.split('; ');
var listaCookies = '';
//bla bla bla
for (var i = 0; i < cookies.length; i++) {
var nombre = cookies[i].substring(0, (cookies[i].indexOf('=')));
var idEliminar = "btn_eliminar_" + nombre;
var close = document.createElement('button');
var span = document.createElement('span');
close.textContent = 'x';
close.className = "eliminar";
span.id = nombre;
span.textContent = cookies[i];
span.appendChild(close);
lista.appendChild(span);
close.addEventListener('click', function(){
//span.remove();
//document.getElementById(nombre).remove();
//close.parentNode.remove();
//this.parentNode.remove();
//HE PROBADO TODO ESTO PERO NADA...
});
}
//bla bla bla
};