Gian Franco Alexis Poma Vidal@pomitax
Como puedo eliminar un elemento de un div contenedor, además quisiera que me respondieran sin la ayuda de jquery y sin los métodos de evento en las etiquetas ya que recien estoy empezando con javascript.
1let contenedor = document.getElementById("contenedor") 2let enlace = document.getElementsByClassName(".enlace") 3 4boton.addEventListener("click",() => { 5 6 let li = document.createElement("li") 7 li.innerHTML = "Contenido " + Math.random() + "<a class='enlace'>x</a>" 8 9 contenedor.appendChild(li) 10}) 11 12enlace.addEventListener("click",(e) => { 13 e.target.parentNode.parentNode.removeChild(e.target.parentNode); //ACÁ ME SALE ERROR 14}) 15body { 16 font-size: sans-serif; 17 max-width: 400px; 18} 19#contenedor li { 20 padding: 18px; 21 margin: 8px 0; 22 background: #ddd; 23 list-style: none; 24} 25#contenedor li a { 26 cursor: pointer; 27 float: right; 28 padding: 3px 7px; 29 background: #fbfbfb; 30} 31<!DOCTYPE html> 32<html> 33 <head> 34 <meta charset="utf-8"> 35 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 36 <title>Page Title</title> 37 <meta name="viewport" content="width=device-width, initial-scale=1"> 38 </head> 39 <body> 40 41 <h1>Como agregar o eliminar elementos a una div</h1> 42 43 <div id="contenedor"> 44 <li>Contenido 1<a>x</a></li> 45 <li>Contenido 2<a>x</a></li> 46 </div> 47 48 <input class="boton" type="button" value="Agregar elemento" style="float: right;"> 49 50 </body> 51</html>~~~
Escribe una respuesta